class Person private String name class Car private

  • Slides: 17
Download presentation

class Person { private String name; class Car { private int position = 0;

class Person { private String name; class Car { private int position = 0; private String driver. Name; public Person(String name){ this. name = name; } public Car(int position, Person driver){ this. position = position; driver. Name = driver. get. Name(); } public String get. Name(){ return name; } public String to. String(){ return driver. Name + " " + position; } } } class Moving. Car. Driver { public static void main(String args[]) { Person alice = new Person("Alice"); Car my. Car = new Car(1, alice); System. out. println(my. Car); } }

class Person { private String name; class Car { private int position = 0;

class Person { private String name; class Car { private int position = 0; private Person driver; public Person(String name){ this. name = name; } public Car(int position, String name){ this. position = position; this. driver = new Person(name); } public String get. Name(){ return name; } public String to. String(){ return driver. get. Name() + " " + position; } } } class Moving. Car. Driver { public static void main(String args[]) { Car my. Car = new Car(1, “Alice”); System. out. println(my. Car); } } Το αντικείμενο δημιουργείται μέσα στον constructor Αυτό έχει νόημα αν το Person χρησιμοποιείται μόνο μέσα στην κλάση Car

class Person { private String name; class Car { private int position = 0;

class Person { private String name; class Car { private int position = 0; private Person driver; public Person(String name){ this. name = name; } public Car(int position, Person driver){ this. position = position; this. driver = driver; } public String get. Name(){ return name; } public String to. String(){ return driver. get. Name() + " " + position; } } } class Moving. Car. Driver { public static void main(String args[]) { Person alice = new Person("Alice"); Car my. Car = new Car(1, alice); System. out. println(my. Car); } } Καλύτερη υλοποίηση!

class Person { private String name; private int age; class Car { private int

class Person { private String name; private int age; class Car { private int position = 0; private Person driver; public Car(int position, Person driver){ this. position = position; if (driver. get. Age() >= 18){ this. driver = driver; } } public Person(String name, int age){ this. name = name; this. age = age; } public String get. Name(){ return name; } public int get. Age(){ return age; } } H Person είναι διαφορετική κλάση άρα δεν μπορούμε να διαβάσουμε το πεδίο age public String to. String(){ return driver. get. Name() + " " + position; } } class Moving. Car. Driver { public static void main(String args[]) { Person alice = new Person("Alice"); Car my. Car = new Car(1, alice); System. out. println(my. Car); } }

class Person { private String name; private int licence; public Person(String name, int licence){

class Person { private String name; private int licence; public Person(String name, int licence){ this. name = name; this. licence = licence; } class Car { private int position = 0; private Person driver; public Car(int position, Person driver){ this. position = position; this. driver = driver; } } } Πως θα υλοποιήσουμε την to. String και την equals?

class Person { private String name; private int licence; class Car { private int

class Person { private String name; private int licence; class Car { private int position = 0; private Person driver; public Car(int position, Person driver){ this. position = position; this. driver = driver; } public Person(String name, int licence){ this. name = name; this. licence = licence; } public String to. String(){ return driver + " " + position; } public String to. String(){ return name + “ “ + licence; } public boolean equals(Person other){ if (this. name. equals(other. name)&& this. licence == other. licence)){ return true }else{ return false; } } } public boolean equals(Car other){ if (this. position == other. position && this. driver. equals(other. driver)){ return true; }else{ return false; } } Φωλιασμένη κλήση της to. String και της equals

class Car { private int position = 0; private Person driver; public Car(int position,

class Car { private int position = 0; private Person driver; public Car(int position, String name){ this. position = position; this. driver = new Person(name); } public String to. String(){ return driver. get. Name() + " " + position; } public Person get. Driver(){ return driver; } } Επιστρέφει το αντικείμενο Person το οποίο είναι ο οδηγός του οχήματος.

class Bank. Account { private String name; private int amount; Μια άλλη επιλογή είναι

class Bank. Account { private String name; private int amount; Μια άλλη επιλογή είναι να δημιουργήσουμε ένα νέο λογαριασμό μετά την συγχώνευση public Bank. Account(String name, int amount){ this. name = name; this. amount = amount; } public void merge(Bank. Account other){ if (this. name. equals(other. name)){ this. amount += other. amount; } Δημιουργούμε ένα νέο αντικείμενο } Bank. Account και το επιστρέφουμε. public Bank. Account merge. Into. New. Account(Bank. Account other){ if (this. name. equals(other. name)){ Bank. Account new. Account = new Bank. Account(name, this. amount+other. amount); return new. Account; } return null; Αν δεν μπορούμε να δημιουργήσουμε το νέο λογαριασμό } } επιστρέφουμε null. Το null είναι το κενό αντικείμενο.