#Objects (java classes)
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @vague pewter! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
- please name your classes and variables in a way that they have a meaning
- I don't understand how you want to add the friend. Should the friend be an instance of class 2 ?
The class Friend is the same as the class person. Therefore you can add another constructor in the class Person which accepts another Person object that you will reference as friend. The class "Friend" is obsolete
private Person friend;
public Person(String firstname, String secondname, Person friend)
{
this.firstname = firstname;
this.secondname = secondname;
this.friend = friend;
}
then you will just want to add a getter and setter for the bestFriend of the person
public Person getBestFriend() {
return this.friend;
}
public void setBestFriend(Person friend) {
this.friend = friend
}
how many classes do you have right now ?
you should only have one class Person
delete the class Friend
why not jump to your real code then because right now your example is breaking oop guidelines
kannst du dann kurz in talk#01 kommen. Dann kannst du mir ausschnitte daraus zeigen und dein Problem besser erklären
oder du erkärst mir dein Problem mit einem anderen Beispiel. Ist aber angenehmer als über Text + Konversationen hier sollten auf Englisch gehalten werden
Der Auftrag wird der Person schon damit zugeordnet, dass du einer instanz der Person einen Auftrag gibst. Auf das Beispiel zurück mit den Personen so müsste einr Methode, die einen Auftrag der Person zuordnet wie folgt aussehen
Person p = new Person("Manuel", " Neuer ");
p.addAuftrag(new Auftrag("Mach was"));
Die einzelnen Aufträge würde ich dann in einer ArrayList speichern oder ggf. in einer Map.
Dies wäre der zweite Constructor von der Klasse Person geworden. So etwas wird als constructor overloading bezeichnet. Den Getter und Setter für den Freund wäre dann die zweite Möglichkeit gewesen einer Person einen Freund hinzuzufügen. Ein Array hier zu nutzen hat den Nachteil, das nur eine gewisse Anzahl von Personen/Aufträgen gespeichert werden können.