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
public interface Predator { public boolean chase. Prey(Prey p); public void eat. Prey(Prey p); } public class Tiger implements Predator { public boolean chase. Prey(Prey p) { // code to chase prey p (specifically for a tiger) return run. After. Prey(p); } public void eat. Prey (Prey p) { // code to eat prey p (for a tiger) chew(p); swallow(p); }. . . } 18
public interface Predator { public boolean chase. Prey(Prey p); public void eat. Prey(Prey p); } public class Shark implements Predator { public boolean chase. Prey(Prey p) { // code to chase prey p (specifically for a shark) return swim. After. Prey(p); } public void eat. Prey (Prey p) { // code to eat prey p //(specifically for a shark) bite(p); swallow(p); } } 19
המסוגל לבצע מספר פעולות , "טרף" הוא גם כן תאור כללי של יצור : גם לו יהיה ממשק. בסיסיות public interface Prey { public boolean is. Alive(); public void die(); public void run. Away(); } : " דוגמאות למחלקות שעשויות לממש "טרף public class Frog implements Prey { public void jump() {. . . } public void run. Away() {. . . } public class Deer implements Prey {. . . } 20
ללא , להוסיף פעולות )שיטות( ומצב )שדות( למחלקות השונות , כמובן , ניתן : לדוגמה. קשר לממשק אותן ממשות public class Shark implements Predator { private String name; private int num. Of. Teeth; public Shark(String name) { this. name = name; num. Of. Teeth = 3000 + (int)(Math. random()*1000); } private void swallow(Prey p) { p. die(); } public int get. Num. Of. Teeth() { return num. Of. Teeth; } public void swim. For. Fun() {. . . } public void eat. Prey (Prey p) { bite(p); swallow(p); }. . . } 24
Exceptions- בעצם כבר נתקלנו ב : Index. Out. Of. Bounds. Exception חריגה ממערך : Null. Pointer. Exception ניסיון לפעול על משתנה שאינו פרימיטיבי בעל ערך null Runtime Exceptions 28
Throw Exceptions public void set. Char(int index, char ch) { if (this. length <= index || index < 0) { throw new Runtime. Exception("Index out of bound"); } else { this. elements[index] = ch; } } public static void main(String[] args) { My. String str = new My. String("Hello world"); str. set. Char(-1, '!'); { Exception in thread "main" java. lang. Runtime. Exception: Index out of bound at My. String. set. Char(My. String. java: 26) at Main. main(Main. java: 35) 29
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) 31
Exceptions סוגי Object Exception IOException Runtime. Exception Null. Pointer. Exception 32
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 34
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 Current speed is 0 km/h 35
- Slides: 35