class Student public class Student private static final
class Student public class Student { private static final int COURSE_PRICE = 1000; private String name; private int id; private int num. Of. Courses; public Student(int id, String name) { this. name = name; this. id = id; num. Of. Courses= 0; } public int compute. Tuition(){ return num. Of. Courses * COURSE_PRICE; } public void set. Num. Of. Courses(int c) { num. Of. Courses = c; } … } //class Student 2
class Milgay public class Milgay extends Student { private int milga; public Milgay(int id, String name, int milga){ super(id, name); this. milga= milga; } public int compute. Tuition(){ return Math. max(0, super. compute. Tuition() – milga); } … } //class Milgay 3
class Student public class Student { private static final int COURSE_PRICE = 1000; private static final int MAX_PRICE = 2500; private String name; private int id; private int num. Of. Courses; public Student(int id, String name) { this. name = name; this. id = id; num. Of. Courses= 0; } public int compute. Tuition (){ return num. Of. Courses * COURSE_PRICE; } public void set. Num. Of. Courses(int c) { num. Of. Courses = c; } public int compute. Payment(){ return Math. min(MAX_PRICE, compute. Tuition()); } } //class Student 5
השפעת השינוי על מלגאי : עתה נריץ את הקוד הבא n Milgay m = new Milgay(12, "Moshe", 750); m. set. Num. Of. Courses(3); System. out. println(m. get. Name() + “ should pay " + m. compute. Payment()); ? כמה ישלם המלגאי 7 n
? private ומה לגבי שיטות : את השיטות Student- נוסיף ל n public void print. Class. Name () {System. out. println(class. Name()); } private String class. Name(){return “Student"; } : נוסיף את השיטה Milgay- ול n private String class. Name(){return “Milgay"; } : ונריץ את הקוד הבא m. print. Class. Name(); student מנגנון הפולימורפיזם לא פועל בשיטות פרטיות 10 n
class Student public class Student { protected static final int COURSE_PRICE = 1000; private String name; private int id; protected int num. Of. Courses; public Student(int id, String name) { this. name = name; this. id = id; num. Of. Courses= 0; } public int compute. Tuition(){ return num. Of. Courses * COURSE_PRICE; } public void set. Num. Of. Courses(int c) { num. Of. Courses = c; } … } //class Student 12
class Milgay public class Milgay extends Student { private int milga; public Milgay(int id, String name, int milga){ super(id, name); this. milga= milga; } public int compute. Tuition. Fee(){ return (num. Of. Courses * COURSE_PRICE) - milga; } … } //class Milgay 13
? אולי ממשקים יעזרו public interface Tank { public double capacity(); public double get. Content(); public void fill(double amount); public boolean is. Full(); } 15
קופסא public class Box implements Tank { private double contents; private double length; private double width; private double height; //… (constructors) public void fill(double amount){ contents = Math. min(contents + amount, capacity()); } public double get. Contents(){ return contents; } 16
קופסא public boolean is. Full(){ return (contents == capacity()); } public double capacity(){ return length*width*height; } } //class Box 17
חבית public class Cylinder implements Tank{ private double contents; private double radius; private double height; //… (constructors) public void fill(double amount){ contents = Math. min(contents + amount, capacity()); } public double get. Contents(){ return contents; } 18
חבית public boolean is. Full(){ return (contents == capacity()); } public double capacity(){ return height * Math. PI * radius; } }//class Cylinder 19
מחלקות מופשטות : הפתרון public abstract class Tank{ private double contents; public abstract double capacity(); 21
מחלקות מופשטות : הפתרון public abstract class Tank{ private double contents; public Tank(){ contents = 0; } public abstract double capacity(); 22
public void fill(double amount){ contents = Math. min(contents + amount, capacity()); } public double get. Contents(){ return contents; } public boolean is. Full(){ return contents == capacity(); } } // class Tank 23
עכשיו ניתן להשתמש בהורשה public class Cylinder extends Tank{ private double radius; private double height; public Cylinder(double radius, double height){ super(); this. radius = radius; this. height = height; } public double capacity(){ return height * Math. PI * radius; } } 24
קופסא class Box extends Tank{ private double length; private double width; private double height; public Box(double length, double width, double height){ super(); this. length = length; this. width = width; this. height = height; } public double capacity(){ return length * width * height; } } 25
קובייה היא גם קופסא class Cube extends Box{ public Cube(double side){ super(side, side); } } 26
? ומה אם public class Tank{ private double contents; public Tank(){ contents = 0; } public abstract double capacity(); Tank should be declared abstract; it does not define capacity() in Tank 27
? ומה אם public abstract class Tank{ private double contents; public Tank(){ contents = 0; } public double capacity(); missing method body, or declare abstract 28
? ומה אם public class Cylinder extends Tank{ private double radius; private double height; public Cylinder(double radius, double height){ super(); this. radius = radius; this. height = height; } } Cylinder should be declared abstract; it does not define capacity() in Tank 29
protected גם כאן אפשר גישה ישירה לשדה Tank- נניח כי אנו רוצים לאפשר למחלקות היורשות מ : contents public abstract class Tank{ protected double contents; … } 31 n
protected גם שיטות יכולות להיות כך שהמחרוזת המודפסת , to. String את Tank- נניח כי אנו רוצים לדרוס ב : קיבולתו ותוכנו , תכלול את סוג המיכל public abstract class Tank{ … public String to. String(){ return concrete. Class. Name() + ", capacity: " + capacity() + ", contents: " + contents; } protected abstract String concrete. Class. Name(); }// class Tank 32 n
. . . ועתה נתקן את המחלקות היורשות public class Box extends Tank{ … protected String concrete. Tank. Class. Name() { return "Box"; } }//class Box public class Cylinder extends Tank{ … protected String concrete. Tank. Class. Name() { return "Cylinder"; } }//class Cylinder 33
ההגדרה של חיה public abstract class Animal { private String name; public String get. Name(){return name; } public Animal (String name){ this. name = name; } abstract public void eat(); abstract public void speak(); } 36
ציפור public class Bird extends Animal { public Bird(String name){ super(name); } public void eat(){ System. out. println(get. Name()+" eats Bissli"); } public void speak(){ System. out. println("Twit twit"); } } 37
יונק public abstract class Mammal extends Animal { public Mammal(String name){ super(name); } public void mammal. Special(){ System. out. println("I’m a mammal!"); } } 38
כלב public class Dog extends Mammal { public Dog(String name){ super(name); } public void eat(){ System. out. println(get. Name()+" eats Dogli"); } public void speak(){ System. out. println("Wouf!"); } } 39
חתול public class Cat extends Mammal { public Cat(String name){ super(name); } public void eat(){ System. out. println(get. Name()+" eats Catli"); } public void speak(){ System. out. println("Meow"); } public void purr(){ System. out. println("Purr"); } } 40
הקמת הגן Scanner sc = new Scanner(System. in); Animal[] zoo = new Animal [10]; for (int i=0; i<zoo. length; i= i+1){ System. out. println("What animal do you want? "); String type = sc. next(); System. out. println(“How would you like to call it? "); if (type. equals("Dog")) zoo[i] = new Dog(sc. next()); else if (type. equals("Cat")) zoo[i] = new Cat(sc. next()); else zoo[i] = new Bird(sc. next()); } 41
אוכל וסיפור for (int i=0; i<zoo. length; i= i+1){ zoo[i]. eat(); zoo[i]. speak(); if (zoo[i] instanceof Mammal){ ((Mammal)zoo[i]). mammal. Special(); if (zoo[i] instanceof Cat) ((Cat)zoo[i]). purr(); } } 42
- Slides: 42