Circle Point public class Point public double x
תזכורת : Circle - ו Point • תזכורת )מההרצאות( – המחלקות public class Point { public double x; public double y; public Point() { x = 0; y = 0; } public Point(Point p) { x = p. x; y = p. y; } בנאי ריק בנאי מעתיק } 3
תזכורת public class Circle{ public Point center; public double radius; //constructors בנאי ריק public Circle() { center = new Point(); radius = 0; } public Circle(Point cen, double rad) { center = new Point(cen); if (rad >= 0) radius = rad; else rad = 0; בנאי עם פרמטרים } … }//Circle 4
Shallow Copy - ו Deep Copy : Circle - • בנאי מעתיק אפשרי ל public Circle(Circle other) { center = other. center; // Copy constructor radius = other. radius; } : • מה יקרה אם נבצע את הפעולות הבאות public static void main(String[] args){ Circle circ 1 = new Circle(); Circle circ 2 = new Circle(circ 1); circ 2. center. x = 4; } . Shallow Copy - • הבנאי המעתיק משתמש בגישת ה 5
Shallow Copy - ו Deep Copy : Circle - • בנאי מעתיק אפשרי ל public Circle(Circle other) { center = new Point(other. center); radius = other. radius; } : • מה יקרה עכשיו אם נבצע את הפעולות הבאות public static void main(String[] args){ Circle circ 1 = new Circle(); Circle circ 2 = new Circle(circ 1); circ 2. center. x = 4; } . Deep Copy - • הבנאי המעתיק משתמש בגישת ה 6
כללים - הורשה מלבד A- ב private יורשת את כל השיטות והמשתנים שאינם B. 5 - לשיטות או שדות פרטיים ב B לא ניתן לגשת מתוך. הבנאים על אף שהם קיימים )בהמשך נראה כיצד ניתן בכל זאת A. (protected כשנדבר על , לאפשר זאת public class Bicycle { private int wheels. Num; } public class Training. Bicycle extends Bicycle { private int training. Wheels. Num; public Training. Bicycle() { wheels. Num=2; // compilation error training. Wheels. Num = 1; } } 14
כללים - הורשה public class Bicycle { private int wheels. Num; public Bicycle(int n) { wheels. Num = n; } } public class Training. Bicycle extends Bicycle { private int training. Wheels. Num; public Training. Bicycle() { super(2); // Calling superclass constr’r training. Wheels. Num = 1; } } 16
כללים - הורשה public class Bicycle { private int wheels. Num; public Bicycle(int n) { wheels. Num = n; } } public class Training. Bicycle extends Bicycle { private int training. Wheels. Num; מה יקרה אם נשמיט את public Training. Bicycle() { ? הקריאה לבנאי training. Wheels. Num = 1; } ! זו שגיאת קומפילציה } no empty constructor for Bicycle: Cannot find symbol: constructor Bicycle() 17
כללים - הורשה public class Bicycle { private int wheels. Num; public Bicycle(int n) { wheels. Num = n; } } public class Training. Bicycle extends Bicycle { private int training. Wheels. Num; public Training. Bicycle() { מה יקרה אם נעביר את training. Wheels. Num = 1; הקריאה לבנאי לשורה super(2); ? השניה } } : הקומפיילר יזהה שתי שגיאות קומפילציה 1. Cannot find symbol: constructor Bicycle() 2. Call to super must be first statement in constructor 18
1 דוגמה public class Bicycle { private int wheels. Num; public Bicycle(int i) { wheels. Num = i; } public int count. Wheels() { return wheels. Num; } } Training. Bicycle - training. Wheels. Num=1 - int count. Wheels() Bicycle - wheels. Num = 2 - int count. Wheels() public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; public Training. Bicycle() { super(2); training. Wheels. Num = 1; } public int count. Wheels() { // overrides Bicycle’s method int reg. Wheels. Num = super. count. Wheels(); return training. Wheels. Num + reg. Wheels. Num; } } 20
2 דוגמה public class Bicycle { private int wheels. Num; public Bicycle(int i) { wheels. Num = i; } public int count. Wheels() { return wheels. Num; } } Training. Bicycle - training. Wheels. Num=1 - int count. Wheels() Bicycle - wheels. Num = 2 - void count. Wheels() public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; מה יקרה אם נשנה את public Training. Bicycle() { ערך ההחזרה בשיטה super(2); ? הדורסת training. Wheels. Num = 1; } public void count. Wheels() { // overrides Bicycle’s method int reg. Wheels. Num = super. count. Wheels(); return training. Wheels. Num + reg. Wheels. Num; } } 21
2 דוגמה public class Bicycle { private int wheels. Num; public Bicycle(int i) { wheels. Num = i; } public int count. Wheels() { return wheels. Num; } } לא ניתן לדרוס שיטה עם חתימה זהה . וטיפוס החזרה שונה זה חוקי כאשר טיפוס ההחזרה : הערה ויורש מטיפוס ההחזרה , הוא אובייקט . של השיטה הנדרסת public class Training. Bicycle extends Bicycle{ private int training. Wheels. Num; public Training. Bicycle() { super(2); training. Wheels. Num = 1; } public void count. Wheels() { // overrides Bicycle’s method int reg. Wheels. Num = super. count. Wheels(); return training. Wheels. Num + reg. Wheels. Num; } } 22
instanceof אופרטור אם ורק אם האובייקט true זהו אופרטור המחזיר instanceof. ( של המחלקה instance) משמאלו הוא מופע : דוגמאות public class Cat {…} public interface Show. Animal {…} public class Siamese extends Cat implements Show. Animal {…} 25
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 26
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 27
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 : שגיאה בזמן ריצה 29
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 : שגיאה בזמן ריצה 30
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; מטפל במקרה ש instanceof } או שאינו מהטיפוס other=null } Point 31
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"); } } 35
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; } } 36
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 page, int recipes){ super(name, page); this. recipes = recipes ; } public void message() { System. out. println(get. Name() + ": " + this. recipes + " recipes "); } } 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); } 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 38
: התייחסות לספרים שונים באופן אחיד לפי יכולות מחלקת האב : פולימורפיזם 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 ? איך בכל זאת ניתן להפעיל שיטה ספציפית : cating בעזרת ((Dictionary)books[0]). get. Definitions(); Casting תכנון נכון של המחלקות יכלול דריסת פונקציות במקום : שימו לב 39
- Slides: 40