public class Training Bicycle extends Bicycle public class
מבוא למדעי המחשב - אוניברסיטת בן גוריון כללים - הורשה ? מה קורה במקרה זה public class Training. Bicycle extends Bicycle { public class Bicycle { private int training. Wheels. Num; private int wheels. Num; public Training. Bicycle() { public Bicycle(int i) { training. Wheels. Num = 1; wheels. Num = i; } } ! זו שגיאת קומפילציה no empty constructor for Bicycle: Cannot find symbol: constructor Bicycle() 12
מבוא למדעי המחשב - אוניברסיטת בן גוריון כללים - הורשה B חייבת להיות השורה הראשונה בבנאי של super הקריאה ל. 8 ? מה קורה במקרה זה public class Training. Bicycle extends Bicycle { public class Bicycle { private int training. Wheels. Num; private int wheels. Num; public Training. Bicycle() { public Bicycle(int i) { training. Wheels. Num = 1; wheels. Num = i; super(2); } } ! שגיאות קומפילציה 2 - הקומפיילר יראה זאת כ 1. Cannot find symbol: constructor Bicycle() 2. Call to super must be first statement in constructor 13
מבוא למדעי המחשב - אוניברסיטת בן גוריון public class Bicycle { private int wheels. Num; public Bicycle(int i) { wheels. Num = i; } public int count. Wheels() { return wheels. Num; } } : 1 דוגמה public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; Training. Bicycle - training. Wheels. Num=1 - int count. Wheels() Bicycle - wheels. Num = 2 - int count. Wheels() public Training. Bicycle() { super(2); training. Wheels. Num = 1; } public int count. Wheels() { // overrides count. Wheels() of Bicycle int reg. Wheels. Num = super. count. Wheels(); //fetch regular # return training. Wheels. Num + reg. Wheels. Num; //return their sum } } 15
מבוא למדעי המחשב - אוניברסיטת בן גוריון public class Bicycle { private int wheels. Num; public Bicycle(int i) { wheels. Num = i; } public int count. Wheels() { return wheels. Num; } } : 2 דוגמה לא ניתן לדרוס שיטה עם חתימה זהה וטיפוס : החזרה שונה Training. Bicycle - training. Wheels. Num=1 - int count. Wheels() Bicycle public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; - wheels. Num = 2 - void count. Wheels() public Training. Bicycle() { super(2); training. Wheels. Num = 1; } public void count. Wheels() { int reg. Wheels. Num = super. count. Wheels(); System. out. println(training. Wheels. Num + reg. Wheels. Num); } } שגיאת קומפילציה The return type is incompatible with Bicycle. count. Wheels() 16
מבוא למדעי המחשב - אוניברסיטת בן גוריון public class Bicycle { private Object wheels. Num; public Bicycle(Object i) { wheels. Num = i; } public Object count. Wheels() { return wheels. Num; } } : 2 דוגמה לא ניתן לדרוס שיטה עם חתימה זהה וטיפוס : החזרה שונה Training. Bicycle - training. Wheels. Num=1 - Object count. Wheels() Bicycle public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; - wheels. Num = 2 - String count. Wheels() public Training. Bicycle() { super(2); training. Wheels. Num = 1; } public String count. Wheels() { Object reg. Wheels. Num = super. count. Wheels(); return reg. Wheels. Num. to. String(); } } זה חוקי כאשר טיפוס ההחזרה הוא אובייקט : הערה 17. ויורש מטיפוס ההחזרה של השיטה הנדרסת
instanceof אופרטור אם ורק אם האובייקט משמאלו הוא true זהו אופרטור המחזיר instanceof. ( של המחלקה מימינו של האופרטור instance) מופע : דוגמאות public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} 20
instanceof אופרטור public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} Cat myaoo = new Cat(); Cat mitzi = new Siamese(); System. out. println(myaoo instanceof Cat); System. out. println(mitzi instanceof Cat); System. out. println(myaoo instanceof "Cat"); //true //Compilation Error System. out. println(mitzi instanceof Siamese); System. out. println(myaoo instanceof Siamese); //true //false System. out. println(mitzi instanceof Show. Animal); //true System. out. println(mitzi instanceof Object); //true 21
instanceof אופרטור public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} Cat myaoo = new Cat(); Cat mitzi = new Siamese(); System. out. println(mitzi instanceof myaoo); //Compilation Error: // operator ! cannot be applied to Cat System. out. println(!(mitzi instanceof Cat)); //false System. out. println(!mitzi instanceof Cat); Cat pitzi = null; System. out. println(pitzi instanceof Cat); System. out. println(null instanceof String); //false 22
equals השיטה : Point עבור equals • נרצה לממש את השיטה public class Point { public double x; public double y; //. . . public boolean equals(Object other) { boolean ans; Point other. Point = (Point)other; ans = (x==other. Point. x) && (y==other. Point. y); return ans; } } ? null הוא other • מה יקרה כאשר Null. Pointer. Exception : שגיאה בזמן ריצה 26
equals השיטה : Point עבור equals • נרצה לממש את השיטה public class Point { public double x; public double y; //. . . public boolean equals(Object other) { boolean ans; Point other. Point = (Point)other; ans = (x==other. Point. x) && (y==other. Point. y); return ans; } } ? Point הוא אינו מטיפוס other • מה יקרה כאשר Class. Cast. Exception : שגיאה בזמן ריצה 27
equals השיטה : Point עבור equals • נרצה לממש את השיטה public class Point { public double x; public double y; //. . . public boolean equals(Object other) { boolean ans = false; if(other instanceof Point) { Point other. Point = (Point)other; ans = (x==other. Point. x) && (y==other. Point. y); } return ans; } } רק כאשר true מחזיר null אינו other Point ומטיפוס 28
Book Dictionary Cookbook public class Book { private String name; private int pages; public Book(String name, int p){ this. name = new String(name); this. pages = p; } public String get. Name(){ return this. name; } public void message() { System. out. println(this. name +": " + this. pages + " pages"); } } 34
Book Dictionary Cookbook /* A Dictionary is a kind of Book with definitions */ public class Dictionary extends Book { private int definitions; public Dictionary(String name, int pages, int defs) { super(name, pages); this. definitions = defs; } public void message() { System. out. println(get. Name() + ": " + definitions + " definitions"); } public int get. Definitions(){ return this. definitions; } } 35
Book Dictionary Cookbook /* A Cook Book is a kind of Book with recipes */ public class Cookbook extends Book { private int recipes; public Cookbook(String name, int pages, int recipes) { super(name, pages); this. recipes = recipes ; } public void message() { System. out. println(get. Name() + ": " + this. recipes + " recipes "); } } 36
שימוש בפולימורפיזם : • התייחסות לספרים שונים באופן אחיד לפי יכולות מחלקת האב public static void main(String[] args) { Book [] books = new Book[3]; // new above is for the array not Book objects books[0] = new Dictionary("Webster“, 690, 45056); books[1] = new Cookbook("Naked Chef“, 100, 50); books[2] = new Book("War and Peace“, 1000); for (int i=0; i<books. length; i=i+1) books[i]. message(); } Output Webster: 45056 definitions Naked Chef: 50 recipes War and Peace: 1000 pages 37
שימוש בפולימורפיזם : • התייחסות לספרים שונים באופן אחיד לפי יכולות מחלקת האב public static void main(String[] args) { Book [] books = new Book[3]; // new above is for the array not Book objects books[0] = new Dictionary("Webster“, 690, 45056); books[1] = new Cookbook("Naked Chef“, 100, 50); books[2] = new Book("War and Peace“, 1000); books[0]. get. Definitions(); // what would this line do ? } // compilation error - no get. Definitions() for Book Casting בעזרת ? איך ניתן בכל זאת להפעיל שיטה ספציפית ((Dictionary)books[0]). get. Definitions(); Casting תכנון נכון של המחלקות יכלול דריסת פונקציות במקום : שימו לב 38
- Slides: 38