1 / 28

תכנות מונחה עצמים בשפת ג'אווה

תכנות מונחה עצמים בשפת ג'אווה. שיעור המשך. והיום בתפריט…. בניית מחלקות: חזרה OOP בשירות הסימולציה הפניות עצמים מורכבים העמסת שיטות. המשימה: סימולציה. המחלקה "איגואנה":. תכונות: - int numOfChildren - double weight - boolean isFriendly שיטות:

cai
Download Presentation

תכנות מונחה עצמים בשפת ג'אווה

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. תכנות מונחה עצמים בשפת ג'אווה שיעור המשך

  2. והיום בתפריט…. • בניית מחלקות: חזרה • OOPבשירות הסימולציה • הפניות • עצמים מורכבים • העמסת שיטות המרכז להוראת המדעים

  3. המשימה: סימולציה המרכז להוראת המדעים

  4. המחלקה "איגואנה": תכונות: - 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) המרכז להוראת המדעים

  5. /** 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( ){ … } … } מבט קצר על קוד המחלקה: כותרת המחלקה תכונות המחלקה שיטה בונה שאר שיטות המחלקה המרכז להוראת המדעים

  6. 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; המרכז להוראת המדעים

  7. שימוש בשיטה : Iguana shmil; shmil = new Iguana(…); Public int numOfChildrenAfter = shmil. numOfChildrenAfterHatching(10,24); המרכז להוראת המדעים

  8. מימוש השיטה הבונה: /** * 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; } המרכז להוראת המדעים

  9. שימוש בשיטה הבונה: Iguana muki = new Iguana(3, 4.2, false); המרכז להוראת המדעים

  10. int capacity int currentAmount intdouble boolean numOfChildren weight isFriendly 4 3 Bucket Iguana מה קורה בזיכרון - חזרה: שלב א': השיטה הבונה מקצה מקום בזיכרון עבור איגואנה חדשה

  11. int capacity int currentAmount intdouble boolean numOfChildren weight isFriendly 4 3 Bucket Iguana מה קורה בזיכרון - חזרה: שלב ב': השיטה הבונה מאתחלת את התכונות , בהתאם לשיטה שכתבנו 3 4.2 false

  12. הצבת עצמים במשתנה: Iguana muki = new Iguana(3, 4.2, false); Iguana shuki = muki; המרכז להוראת המדעים

  13. intdouble boolean numOfChildren weight isFriendly intdouble boolean numOfChildren weight isFriendly 3 3 4.2 4.2 false false הצבת עצמים במשתנים - המשך: Iguana muki = Iguana shuki = המרכז להוראת המדעים

  14. הבעיה: איך אותו עצם יכול להיות בשני מקומות? הפתרון: הפניה (reference) המרכז להוראת המדעים

  15. intdouble boolean numOfChildren weight isFriendly 3 4.2 false משתנים מכילים עצמים על-ידי הפניה (reference): Iguana muki Iguana shuki המרכז להוראת המדעים

  16. intdouble boolean numOfChildren weight isFriendly 3 4.2 false משתנים מכילים עצמים על-ידי הפניה (reference): Iguana muki Iguana shuki המרכז להוראת המדעים

  17. intdouble boolean numOfChildren weight isFriendly Iguana 3 4.2 false שטח זיכרון א': Iguana muki Iguana shuki שטח זיכרון ב':

  18. כמה מילים על "קונסטרקטור ברירת מחדל" הגדרה: שיטה בונה שאינה מקבלת פרמטרים הערה: אם לא הוספנו למחלקה שיטה בונה ג'אווה תוסיף שיטת ברירת מחדל המרכז להוראת המדעים

  19. עצמים מורכבים מעצמים המרכז להוראת המדעים

  20. המחלקה Date: המרכז להוראת המדעים

  21. /** 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( ){ … } … } תוספת לקוד המחלקה: תכונה חדשה המרכז להוראת המדעים

  22. 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); המרכז להוראת המדעים

  23. 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()) המרכז להוראת המדעים

  24. השיטה: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; } המרכז להוראת המדעים

  25. date2 (this.birthday).comesBefore( ) date2 מה תבצע הפקודה:iguana1.isOlderThan(igauan2) iguana2.getBirthday( ) true/false date1 true/false המרכז להוראת המדעים

  26. העמסת שיטות overloading המרכז להוראת המדעים

  27. סיכום ההרצאה : • משתנים מכילים עצמים על-ידי הפניה (reference ) • הפניה מחייבת זהירות • עצמים יכולים להיות מורכבים מעצמים • קונסטרקטור ברירת מחדל (default constructor) • הערך null • העמסת שיטות: ניתן לקרוא לשיטות שונות באותו • שם – בתנאי שיש להן רשימת פרמטרים שונה המרכז להוראת המדעים

  28. - סוף -

More Related