this Shallow Copy Deep Copy Exceptions 2 public
היום בתרגול this Shallow Copy, Deep Copy שיטות מיוחדות (Exceptions) חריגות 2
מחלקות ואובייקטים : תזכורת public class My. String { : My. String //class fields private char[] elements; private int length; //class method public int length(){ return length; } הנה המחלקה שדות – מייצגים מצב של האובייקט שיטה – מתארת התנהגות של האובייקט } 3
(constructor) בנאי : תזכורת public } בנאי – שיטה מיוחדת עבור מחלקה שתפקידה לייצר אובייקט ולאתחל את שדותיו class My. String { //class fields השם זהה לשם private char[] elements; המחלקה ואין ערך private int length; החזרה //constructor with a parameter public My. String(char[] other. Elements){ length = other. Elements. length; elements = new char[length]; for (int i = 0; i < other. Elements. length; i++) { elements[i] = other. Elements[i]; } } . . . 5
this ? מה עושה הבנאי השני : דוגמה נוספת public class My. String{ private char[] elements; private int length; public My. String(String s) { … } public My. String() { this(""); … } } 7
this על מנת להבדיל בין שדה לבין this - ניתן להשתמש ב : לדוגמה. פרמטר או משתנה לוקאלי בעלי אותו שם public class My. String{ private char[] elements; private int length; (. ) בעזרת האופרטור public My. String(char[] elements, int length) { this. length = length; this. elements = new char[this. length]; for (int i = 0; i < this. length; i=i+1) this. elements[i] = elements[i]; } } 8
תזכורת : 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; } } 9
תזכורת 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 10
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 - • הבנאי המעתיק משתמש בגישת ה 11
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 - • הבנאי המעתיק משתמש בגישת ה 12
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 : שגיאה בזמן ריצה 16
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 : שגיאה בזמן ריצה 17
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 18
Throw Runtime. Exception public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void set. Speed(int speed){ if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) this. speed = speed; else throw new Runtime. Exception(“Illegal speed”); } } public static void main(String[] args) { Car car = new Car(); car. set. Speed(300); } Output: Exception in thread "main" java. lang. Runtime. Exception: Illegal Speed at Car. set. Speed(Car. java: 11) at Car. main(Car. java: 17) 24
Exceptions סוגי Object Exception IOException Runtime. Exception Null. Pointer. Exception 25
Throw and Catch Exceptions public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void set. Speed(int speed) throws Exception { if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) this. speed = speed; else throw new Exception(“Illegal speed”); } } public static void main(String[] args) { Car car = new Car(); car. set. Speed(100); } Compilation Error 27
Throw and Catch Exceptions public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void set. Speed(int speed) throws Exception { if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) this. speed = speed; else throw new Exception(“Illegal speed”); } } public static void main(String[] args) { Car car = new Car(); try{ car. set. Speed(300); System. out. println("Broke the speed limit !"); } catch(Exception e){ System. err. println("Caught Exception: "+e. get. Message()); } System. out. println("Current speed is "+car. get. Speed()+” km/h); } Output: Caught Exception: Illegal Speed 28 Current speed is 0 km/h
- Slides: 27