public class My String class fields private char
מחלקות ואובייקטים : תזכורת public class My. String { //class fields private char[] elements; private int length; //class method public int length(){ return length; } : My. String • המחלקה שדות – מייצגים מצב של האובייקט שיטה – מתארת התנהגות של האובייקט } 4
(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]; } } } . . . 6
this ? מה עושה הבנאי השני : • דוגמה נוספת public class My. String{ private char[] elements; private int length; public My. String(String s) { … } public My. String() { this(""); … } } 8
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]; } } 9
תזכורת : 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; } } 10
תזכורת 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 11
Shallow Copy - ו Deep Copy : Circle - • בנאי מעתיק אפשרי ל public Circle(Circle other) { center = other. center; constructor radius. Copy = 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 - • הבנאי המעתיק משתמש בגישת ה 12
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 - • הבנאי המעתיק משתמש בגישת ה 13
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); }. . . } 19
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); } } 20
המסוגל לבצע מספר פעולות , • "טרף" הוא גם כן תאור כללי של יצור : גם לו יהיה ממשק. בסיסיות public interface Prey { public boolean is. Alive(); public void run. Away(); public void die(); } : " • דוגמאות למחלקות שעשויות לממש "טרף public class Frog implements Prey { public void jump() {. . . } public void run. Away() {. . . } public class Deer implements Prey {. . . } 21
המשך Predator sharky 1 = new Shark(); Prey kermit 1 = new Frog(); simulate. Chase(sharky 1, kermit 1); : ניתן גם לרשום , שימו לב Shark sharky 2 = new Shark(); Frog kermit 2 = new Frog(); simulate. Chase(sharky 2, kermit 2); 24
ללא , להוסיף פעולות )שיטות( ומצב )שדות( למחלקות השונות , כמובן , • ניתן : לדוגמה. קשר לממשק אותו הן מממשות 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); }. . . 25
Predator { public boolean chase. Prey(Prey p){ return true; } public void eat. Prey(Prey p); }
public class Tiger implements Predator { private int speed; private int x; public void run() { x += speed; } public boolean chase. Prey(Prey p) { return false; } } public void eat. Prey(Prey p) { }
public interface Prey { private boolean is. Alive(); public void run. Away(); public void die(); }
public class Fly implements Prey { public boolean is. Alive() { return false; } public void run. Away() {} public void die() {} }
public class Frog implements Predator, Prey { private int color; private int speed; public boolean chase. Prey(Prey p) { return false; } public void eat. Prey(Prey p) {} public boolean is. Alive() { return false; } Where } public void run. Away() {} is Die() from Prey? !
public class Main { public static void main(String[] args) { Predator p = new Predator(); Predator bzzit = new Fly(); } } Predator tiger = new Tiger(); tiger. run();
- Slides: 34