this Encapsulation visibility modifiers Setters Getters Exceptions 2
היום בתרגול this (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 { //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]; } } . . . 5
this ? מה עושה הבנאי השני : דוגמה נוספת public class My. String{ public char[] elements; public int length; public My. String(String s) { … } public My. String() { this(""); … } } 7
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]; } } 8
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 12
Visibility Modifiers public class Car { … //we are not FRAIERIM public double get. Price() { return get. Market. Price()*1. 1; } private double get. Market. Price(){. . . } } 17
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); }. . . } 24
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); } } מבוא למדעי המחשב - אוניברסיטת בן גוריון 25
המסוגל לבצע מספר פעולות , "טרף" הוא גם כן תאור כללי של יצור : גם לו יהיה ממשק. בסיסיות 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 {. . . } 26
ללא , להוסיף פעולות )שיטות( ומצב )שדות( למחלקות השונות , כמובן , ניתן : לדוגמה. קשר לממשק אותן ממשות 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); }. . . } 30
Exceptions- בעצם כבר נתקלנו ב : Index. Out. Of. Bounds. Exception חריגה ממערך : Null. Pointer. Exception ניסיון לפעול על משתנה שאינו פרימיטיבי בעל ערך null Runtime Exceptions 34
Throw 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(Main. java: 11) at Car. main(Main. java: 17) 35
- Slides: 35