this Shallow Copy Deep Copy Encapsulation visibility modifiers
היום בתרגול this Shallow Copy, Deep Copy (Encapsulation) הכמסה (visibility modifiers) הגבלת גישה Setters - ו Getters (Exceptions) חריגות 2
מחלקות ואובייקטים : תזכורת public class My. String { //class fields public char[] elements; public int length; //class method public int length(){ return length; } : My. String הנה המחלקה שדות – מייצגים מצב של האובייקט שיטה – מתארת התנהגות של האובייקט } 3
(constructor) בנאי : תזכורת public בנאי – שיטה מיוחדת עבור מחלקה שתפקידה לייצר אובייקט ולאתחל את שדותיו class My. String { public char[] elements; public int length; השם זהה לשם המחלקה ואין ערך החזר public My. String(String s){ length = s. length(); elements = new char[length]; for (int i = 0; i < s. length(); i++) { elements[i] = s. char. At(i); } } } … : ישירות My. String ועכשיו נוכל לאתחל אובייקט מסוג My. String s = new My. String("bimba"); 5
(constructor) בנאי : תזכורת : נוסיף בנאים אחרים public class My. String { //class fields public char[] elements; public 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]; } } } . . . 6
בנאי ברירת מחדל : תזכורת (default constructor) לא יסופק לנו בנאי חסר , ברגע שמגדירים בנאי כלשהו פרמטרים public class My. String { //class fields public char[] elements; public int length; //constructor with a parameter public My. String(char[] other. Elements){ … } } : מה יקרה כאשר נבצע My. String s 1 = new My. String(); שגיאת קומפילציה 7
בנאי מעתיק : תזכורת (copy constructor) public My. String(My. String other) { length = other. length; elements = new char[length]; for (int i = 0; i < length; i=i+1) elements[i] = other. elements[i]; } מעתיקים את השדות של האובייקט ( )העתקת המצב : משתמשים בבנאי זה באופן הבא My. String s 1 = new My. String(“Bob”); My. String s 2 = new My. String(s 1); 8
this ? מה עושה הבנאי השני : דוגמה נוספת public class My. String{ public char[] elements; public int length; public My. String(String s) { … } public My. String() { this(""); … } } 10
this על מנת להבדיל בין שדה לבין this - ניתן להשתמש ב : לדוגמה. פרמטר או משתנה לוקאלי בעלי אותו שם public class My. String{ public char[] elements; public 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]; } } 11
Shallow Copy - ו Deep Copy : 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; } } 12
Shallow Copy - ו Deep Copy 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 13
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 - • הבנאי המעתיק משתמש בגישת ה 14
Shallow Copy - ו Deep Copy : Circle - • בנאי מעתיק אחר ל public Circle(Circle other) { center = new Point(other. center); radius = other. radius; } : • מה יקרה עכשיו אם נבצע את הפעולות הבאות Circle circ 1 = new Circle(); Circle circ 2 = new Circle(circ 1); circ 2. center. x = 4; . Deep Copy - • הבנאי המעתיק הזה משתמש בגישת ה 15
equals השיטה Point עבור המחלקה equals נממש מחדש את השיטה public class Point { public double x; public double y; רק אם true מחזיר Null אינו other Point ומטיפוס public boolean equals(Object other){ boolean ans = false; if (other instanceof Point){ Point other. Point = (Point)other; if (x==other. Point. x && y==other. Point. y) ans = true; } return ans; } } 17
Setters - ו Getters : דוגמה – קביעת מהירות במכונית public class Car { public final int MAX_SPEED = 210; public final int MIN_SPEED = -20; public int speed; קבועים של המחלקה public int get. Speed() { return speed; } public void set. Speed(int speed) { if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)){ this. speed = speed; } } } getter setter 22
Visibility Modifiers public class Car { … //we are not FRAIERIM public double get. Price() { return get. Market. Price()*1. 1; } private double get. Market. Price(){. . . } } 27
Setters - ו Getters ולא נרצה , נניח שישנו קוד הפעלה למכונית : דוגמה נוספת לחשוף אותו public class Car { private String code; … public Car(String initial. Code) { code = initial. Code; } public void start. Car(String code) { if (!code. equals(this. code)) System. out. println("Wrong code"); else … } } 29
Setters - ו Getters ( )המשך משקף קודם public class Car { … public void set. New. Code(String old. Code, String new. Code) { if (!old. Code. equals(code)) System. out. println(“Wrong code”); else code = new. Code; } } (Getter )ללא Setter הגדרנו רק 30
Exceptions- בעצם כבר נתקלנו ב : Arithmetic. Exception ניסיון חלוקה באפס : Index. Out. Of. Bounds. Exception חריגה ממערך : Null. Pointer. Exception null ניסיון לפעול על מערך בעל ערך Runtime Exceptions 32
Exceptions סוגי Exception IOException Runtime. Exception Null. Pointer. Exception 33
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){ 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) 35
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 36
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); } catch(Exception e){ System. err. println("Caught Exception: " + e. get. Message()); } Output: Caught Exception: Illegal Speed } 37
- Slides: 37