constructor public class My String class fields public
(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 מבוא למדעי המחשב - אוניברסיטת בן גוריון
this ? מה עושה הבנאי השני : דוגמה נוספת public class My. String{ public char[] elements; public int length; public My. String(String s) { … } public My. String() { this(""); … } } 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; } } } 13 מבוא למדעי המחשב - אוניברסיטת בן גוריון getter setter
Visibility Modifiers public class Car { … //we are not FRAIERIM public double get. Price() { return get. Market. Price()*1. 1; } private double get. Market. Price(){. . . } } 18 מבוא למדעי המחשב - אוניברסיטת בן גוריון
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); }. . . } 25 מבוא למדעי המחשב - אוניברסיטת בן גוריון
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); } } 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); }. . . } 31 מבוא למדעי המחשב - אוניברסיטת בן גוריון
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); } 37 Output: Exception in thread "main" java. lang. Runtime. Exception: Illegal Speed at Car. set. Speed(Main. java: 11) at Car. main(Main. java: 17) מבוא למדעי המחשב - אוניברסיטת בן גוריון
- Slides: 37