public class User private String username private String
מבוא למדעי המחשב - אוניברסיטת בן גוריון כללים - הורשה ? מה קורה במקרה זה public class User{ private String username; private String password; public User(String usr, String pass) { username = usr; password = pass; } } public class Student extends User{ private double debt; public Student(String usr, String pass) { debt = 0; } } ! זו שגיאת קומפילציה no empty constructor for User: Cannot find symbol: constructor User() 12
מבוא למדעי המחשב - אוניברסיטת בן גוריון כללים - הורשה B חייבת להיות השורה הראשונה בבנאי של super הקריאה ל. 8 ? מה קורה במקרה זה public class User{ private String username; private String password; public User(String usr, String pass) { username = usr; password = pass; } } public class Student extends User{ private double debt; public Student(String usr, String pass) { debt = 0; super(usr, pass); } } ! שגיאות קומפילציה 2 - הקומפיילר יראה זאת כ 1. Cannot find symbol: constructor User() 2. Call to super must be first statement in constructor 13
public class User{ … private int funds; public User(String usr, String pass) { username = usr; password = pass; funds = 0; } public int calc. Balance(){ return funds; } } public class Student extends User{ private int debt; public Student (String usr, String pass) { super(usr, pass); debt = 0; } } // overrides calc. Balance() of User public int calc. Balance(){ return super. calc. Balance() - debt; } } מבוא למדעי המחשב - אוניברסיטת בן גוריון Student - debt= 50000 - int calc. Balance() User - funds = 0 - int calc. Balance () 15
מבוא למדעי המחשב - אוניברסיטת בן גוריון public class User{ … לא ניתן לדרוס שיטה עם חתימה זהה וטיפוס private int funds; : החזרה שונה public User(String usr, String pass) { username = usr; password = pass; funds = 0; Student } - debt= 50000 public void calc. Balance(){ - void calc. Balance() return funds; } User } - funds = 0 - int calc. Balance () public class Student extends User{ private int debt; public Student (String usr, String pass) { super(usr, pass); debt = 0; קומפילציה } שגיאת } The return type is incompatible with User. calc. Balance() // overrides calc. Balance() of User public int calc. Balance(){ זה חוקי כאשר טיפוס : הערה return super. calc. Balance() - debt; ההחזרה הוא אובייקט ויורש מטיפוס } 16. ההחזרה של השיטה הנדרסת }
instanceof אופרטור אם ורק אם האובייקט משמאלו הוא true זהו אופרטור המחזיר instanceof. ( של המחלקה מימינו של האופרטור instance) מופע : דוגמאות public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} 19
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 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(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 21
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 : שגיאה בזמן ריצה 25
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 : שגיאה בזמן ריצה 26
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; } } אינו other רק כאשר true מחזיר Point ומטיפוס null 27
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"); } } 33
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; } } 34
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 "); } } 35
שימוש בפולימורפיזם : • התייחסות לספרים שונים באופן אחיד לפי יכולות מחלקת האב 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 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); books[0]. get. Definitions(); // what would this line do ? } // compilation error - no get. Definitions() for Book Casting בעזרת ? איך ניתן בכל זאת להפעיל שיטה ספציפית ((Dictionary)books[0]). get. Definitions(); Casting תכנון נכון של המחלקות יכלול דריסת פונקציות במקום : שימו לב 37
- Slides: 37