Code: Select all
public class Brother {
private final Parent parent;
public Brother(Parent parent) {
this.parent = parent;
}
public void annoySister() {
parent.getSister().annoy();
}
}
public class Sister {
private final Parent;
public Sister(Parent parent) {
this.parent = parent;
}
public void annoy() {
System.out.println("I am annoyed");
}
public void parentsName() {
parent.getName();
}
}
public class Parent {
private final Brother brother;
private final Sister sister;
private final String name;
public Parent() {
this.name = "Johnny";
this.brother = new Brother(this);
this.sister = new Sister(this);
}
// getters
}
< /code>
Auf diese Weise stehen alle in der Elternklasse erstellten Objekte miteinander zur Verfügung. In dem obigen Code kann der Bruder beispielsweise auf Schwestermethoden zugreifen und umgekehrt, zusätzlich zu den von der Elternklasse bereitgestellten Methoden (im Wesentlichen auf seinen Zustand). < /P>
public class Main {
public static void main(String args) {
// somehow this seems to be like a container for all objects; ApplicationContext?
Parent parentInstance = new Parent();
parentInstance.getBrother().annoySister();
parentInstance.getSister().parentsName();
}
}
< /code>
q. [b] Wie kann dies