ג'אווה - תכנות מונחה עצמים - משתנים מטיפוס מחלקה והמרת טיפוסים למעלה/למטה
תכנות מונחה עצמים בשפת ג'אווה
description
Transcript of תכנות מונחה עצמים בשפת ג'אווה
תכנות מונחה עצמים בשפת ג'אווה
שיעור המשך
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המרכז להוראת המדעים
מימוש שיטת החישוב למספר הצאצאים:/** *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. */public int numOfChildrenAfterHatching (int numOfEggsLayed , double precentOfHatching){
{
//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המרכז להוראת המדעים
מימוש השיטה הבונה:
public Iguana(int numOfChildren , double weight ,
boolean isFriendly){
this.numOfChildren = numOfChildren;
this.weight = weight;
this.isFriendly = isFriendly;
}
/** * constructs a new Iguana, according to all given parameters */
9המרכז להוראת המדעים
שימוש בשיטה הבונה:
Iguana muki = new Iguana(3, 4.2, false);
מה קורה בזיכרון - חזרה:
שלב א': השיטה הבונה מקצה מקום בזיכרון עבור
איגואנה חדשה
int capacity int currentAmount
Bucket
34
int double boolean numOfChildren weight isFriendly
Iguana
מה קורה בזיכרון - חזרה:
שלב ב': השיטה הבונה מאתחלת את
התכונות , בהתאם לשיטה שכתבנו
int capacity int currentAmount
Bucket
34
int double boolean numOfChildren weight isFriendly
Iguana
false4.23
12המרכז להוראת המדעים
הצבת עצמים במשתנה:
Iguana muki = new Iguana(3, 4.2, false); Iguana shuki = muki;
13המרכז להוראת המדעים
הצבת עצמים במשתנים - המשך:
Iguana muki =
int double boolean numOfChildren weight isFriendly
3 4.2 false
Iguana shuki =
int double boolean numOfChildren weight isFriendly
3 4.2 false
14המרכז להוראת המדעים
הבעיה:
? מקומות בשני להיות יכול עצם אותו איך
הפתרון:
(reference)הפניה
15המרכז להוראת המדעים
משתנים מכילים עצמים על-ידי הפניה (reference :)
Iguana muki
Iguana shuki
int double boolean numOfChildren weight isFriendly
3 4.2 false
16המרכז להוראת המדעים
משתנים מכילים עצמים על-ידי הפניה (reference :)
Iguana muki
Iguana shuki
int double boolean numOfChildren weight isFriendly
3 4.2 false
שטח זיכרון א':
int double boolean numOfChildren weight isFriendly
Iguana
false4.23
:' ב זיכרון שטח
Iguana muki
Iguana shuki
18המרכז להוראת המדעים
כמה מילים על "קונסטרקטור ברירת מחדל"
מקבלת הגדרה: שאינה בונה שיטהפרמטרים
אם לא הוספנו למחלקההערה: שיטה בונה ג'אווה תוסיף
שיטת ברירת מחדל
19המרכז להוראת המדעים
עצמים מורכבים מעצמים
20המרכז להוראת המדעים
:Dateהמחלקה Method Signatures:
Date (int day, int month, int year)
int getDay()
int getMonth()
int getYear()
void setDay(int day)
void setMonth(int month)
void setYear(int year)
boolean comesBefore(Date d2)
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המרכז להוראת המדעים
שיטת אחזור התאריך: /** * returns the Iguana’s birthday. * It returns only a copy of birthday and not original one */
}
public Date getBirthday{)(
int day = (this.birthday).getDay( ); int month = (this.birthday).getMonth( );int year = (this.birthday).getYear( );//return a new Date object: return new Date(day, month,year);
23המרכז להוראת המדעים
שיטה בונה חדשה: /** * constructs a new Iguana according to given parameters * the birthday parameter is copied, and not taken as such */
}
public Iguana (int numOfChildren, double weight,
boolean isFriendly, Date birthday{)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המרכז להוראת המדעים
מה תבצע iguana1.isOlderThan(igauan2)הפקודה: iguana2.getBirthday( )
date2
(this.birthday).comesBefore( )
date2
date1true/falsetrue/false
26המרכז להוראת המדעים
העמסת שיטות overloading
27המרכז להוראת המדעים
:סיכום ההרצאה
משתנים מכילים עצמים על-ידי הפניה •(reference )
הפניה מחייבת זהירות•
עצמים יכולים להיות מורכבים מעצמים •
default קונסטרקטור ברירת מחדל )•constructor)
null הערך •
ניתן לקרוא לשיטות שונות העמסת שיטות: •באותו
שם – בתנאי שיש להן רשימת פרמטרים שונה
- סוף -