290 likes | 582 Views
תכנות מונחה עצמים בשפת ג'אווה. שיעור המשך. והיום בתפריט…. בניית מחלקות: חזרה OOP בשירות הסימולציה הפניות עצמים מורכבים העמסת שיטות. המשימה: סימולציה. המחלקה "איגואנה":. תכונות: - int numOfChildren - double weight - boolean isFriendly שיטות:
E N D
תכנות מונחה עצמים בשפת ג'אווה שיעור המשך
והיום בתפריט…. • בניית מחלקות: חזרה • OOPבשירות הסימולציה • הפניות • עצמים מורכבים • העמסת שיטות המרכז להוראת המדעים
המשימה: סימולציה המרכז להוראת המדעים
המחלקה "איגואנה": תכונות: - int numOfChildren - double weight - boolean isFriendly שיטות: Iguana (int numOfChildren, double weight, boolean isFriendly) int getNumOfChildren( ) void eat(int numOfSeaweeds) int numOfChildrenAfterHatching (int numOfEggsLayed, double precentOfHatching) המרכז להוראת המדעים
/** This class represents an Iguana */ public class Iguana{ private int numOfChildren; private boolean isFriendly; private double weight; public Iguana(int numOfChildren, double weight, boolean isFriendly){ … } public int getNumOfChildren( ){ … } … } מבט קצר על קוד המחלקה: כותרת המחלקה תכונות המחלקה שיטה בונה שאר שיטות המחלקה המרכז להוראת המדעים
public int numOfChildrenAfterHatching (int numOfEggsLayed , double precentOfHatching){ } מימוש שיטת החישוב למספר הצאצאים: /** *A method that simulates future hatching of the Iguana * it receives the number of eggs laid by the Iguana, * the percentage of them should hatch, and updates the number of * children accordingly. It also returns the updated number of children. */ //calculate how many eggs actually hatched: int eggsHatched = (int)( numOfEggsLayed * precentOfHatching/100) ; //update num of children: this.numOfChildren = this.numOfChildern + eggsHatched; //return the updated number: return this.numOfChildren; המרכז להוראת המדעים
שימוש בשיטה : Iguana shmil; shmil = new Iguana(…); Public int numOfChildrenAfter = shmil. numOfChildrenAfterHatching(10,24); המרכז להוראת המדעים
מימוש השיטה הבונה: /** * constructs a new Iguana, according to all given parameters */ public Iguana(int numOfChildren , double weight , boolean isFriendly){ this.numOfChildren = numOfChildren; this.weight = weight; this.isFriendly = isFriendly; } המרכז להוראת המדעים
שימוש בשיטה הבונה: Iguana muki = new Iguana(3, 4.2, false); המרכז להוראת המדעים
int capacity int currentAmount intdouble boolean numOfChildren weight isFriendly 4 3 Bucket Iguana מה קורה בזיכרון - חזרה: שלב א': השיטה הבונה מקצה מקום בזיכרון עבור איגואנה חדשה
int capacity int currentAmount intdouble boolean numOfChildren weight isFriendly 4 3 Bucket Iguana מה קורה בזיכרון - חזרה: שלב ב': השיטה הבונה מאתחלת את התכונות , בהתאם לשיטה שכתבנו 3 4.2 false
הצבת עצמים במשתנה: Iguana muki = new Iguana(3, 4.2, false); Iguana shuki = muki; המרכז להוראת המדעים
intdouble boolean numOfChildren weight isFriendly intdouble boolean numOfChildren weight isFriendly 3 3 4.2 4.2 false false הצבת עצמים במשתנים - המשך: Iguana muki = Iguana shuki = המרכז להוראת המדעים
הבעיה: איך אותו עצם יכול להיות בשני מקומות? הפתרון: הפניה (reference) המרכז להוראת המדעים
intdouble boolean numOfChildren weight isFriendly 3 4.2 false משתנים מכילים עצמים על-ידי הפניה (reference): Iguana muki Iguana shuki המרכז להוראת המדעים
intdouble boolean numOfChildren weight isFriendly 3 4.2 false משתנים מכילים עצמים על-ידי הפניה (reference): Iguana muki Iguana shuki המרכז להוראת המדעים
intdouble boolean numOfChildren weight isFriendly Iguana 3 4.2 false שטח זיכרון א': Iguana muki Iguana shuki שטח זיכרון ב':
כמה מילים על "קונסטרקטור ברירת מחדל" הגדרה: שיטה בונה שאינה מקבלת פרמטרים הערה: אם לא הוספנו למחלקה שיטה בונה ג'אווה תוסיף שיטת ברירת מחדל המרכז להוראת המדעים
עצמים מורכבים מעצמים המרכז להוראת המדעים
המחלקה Date: המרכז להוראת המדעים
/** This class represents an Iguana */ public class Iguana{ private int numOfChildren; private boolean isFriendly; private double weight; private Date birthday; public Iguana(int numOfChildren, double weight, boolean isFriendly){ … } public int getNumOfChildren( ){ … } … } תוספת לקוד המחלקה: תכונה חדשה המרכז להוראת המדעים
public Date getBirthday(){ } שיטת אחזור התאריך: /** * returns the Iguana’s birthday. * It returns only a copy of birthday and not original one */ int day = (this.birthday).getDay( ); int month = (this.birthday).getMonth( ); int year = (this.birthday).getYear( ); //return a new Date object: returnnew Date(day, month,year); המרכז להוראת המדעים
public Iguana (int numOfChildren, double weight, boolean isFriendly, Date birthday){ זהירות! סוס טרויאני! } שיטה בונה חדשה: /** * constructs a new Iguana according to given parameters * the birthday parameter is copied, and not taken as such */ this.numOfChildren = numOfChildren; this.weight = weight; this.isFriendly = isFriendly; this.birthday = birthday; this.birthday = new Date(birthday.getDay(), birthday.getMonth(), birthday.getYear()) המרכז להוראת המדעים
השיטה:boolean isOlderThan(Iguana iguana2) /** * A method that returns true if and only if this * Iguana is older (or same age) as iguana2. */ public boolean isOlderThan(Iguana iguana2){ //find iguana2's birthday, and store in "birthday2": Date birthday2 = iguana2.getBirthday(); //comapre the two birthdays (using class Date's comparing // method),and store it in "isOlder“: boolean isOlder = (this.birthday).comesBefore (birthday2); //return the result: return isOlder; } המרכז להוראת המדעים
date2 (this.birthday).comesBefore( ) date2 מה תבצע הפקודה:iguana1.isOlderThan(igauan2) iguana2.getBirthday( ) true/false date1 true/false המרכז להוראת המדעים
העמסת שיטות overloading המרכז להוראת המדעים
סיכום ההרצאה : • משתנים מכילים עצמים על-ידי הפניה (reference ) • הפניה מחייבת זהירות • עצמים יכולים להיות מורכבים מעצמים • קונסטרקטור ברירת מחדל (default constructor) • הערך null • העמסת שיטות: ניתן לקרוא לשיטות שונות באותו • שם – בתנאי שיש להן רשימת פרמטרים שונה המרכז להוראת המדעים