Hallo!
Ich verstehe nicht, worauf sich this in unter stehendem Code bezieht. Könnte mir jemand das erklären, bitte? Debugging hilft nicht, es schreibt einfach, dass this = B ist. Also ich kann vermuten, dass this ein Instanz von Subklasse B ist, aber ich verstehe die Zuweisung von Werten an this nicht. Warum wird es zuerst null und dann "Otto of type B".
Output:
Danke im Voraus!
Ich verstehe nicht, worauf sich this in unter stehendem Code bezieht. Könnte mir jemand das erklären, bitte? Debugging hilft nicht, es schreibt einfach, dass this = B ist. Also ich kann vermuten, dass this ein Instanz von Subklasse B ist, aber ich verstehe die Zuweisung von Werten an this nicht. Warum wird es zuerst null und dann "Otto of type B".
Java:
class A { //subclass
private String stringRepr;
A(String s) {
stringRepr = s + " of type A";
System.out.println("I am " + this);
}
public String toString() {
return stringRepr;
}
}
public class B extends A { //superclass
private String stringRepr;
B(String s) {
super(s);
stringRepr = s + " of type B";
System.out.println("I am " + this);
}
public String toString() {
return stringRepr;
}
public static void main(String[] arg) {
System.out.println("instantiate B");
B b = new B("Otto");
}
}
Output:
Java:
instantiate B
I am null
I am Otto of type B
Danke im Voraus!