public class Link {
public static Link LAST = new Link(-1);
public Link next;
public int myId;
public Link() {
this(10);
}
public Link(Link next) {
this(20);
this.next = next;
}
protected Link(boolean b) {
this(99);
}
private Link(int myId) {
this.myId = myId;
}
@Override
public String toString() {
return "Link #" + myId;
}
}
public class DataLink<T> extends Link {
private T value;
public DataLink(T value) {
this(value, new Link(false));
}
public DataLink(T value, Link parent) {
super(parent);
this.value = value;
}
@Override
public String toString() {
return super.toString() + " (" + value.toString() +")";
}
}
public class Main {
public static void main(String[] args) {
************************
}
}
代替 ************************
system . out。println(新DataLink<>(新的链接()). next),
打印Link # 99,它是如何到达protected Link(Boolean b)的?
您可以使用任何JAVA IDE来跟踪这些步骤
new DataLink<>(new Link()).next
获取DataLink的父类( LINK )的NEXT LINK对象,因为DataLink类没有NEXT对象。
这是步骤
1.new DataLink<>(new Link())
2.this(value, new Link(false));
3.protected Link(boolean b) { //from new Link(false)
this(99);
4. this.myId = myId; // myId = 99, from this(99)
5.super(parent); //calling public Link(Link next)
6.this.next = next; //next = link object with myId=99
所以当你调用new DataLink<>(new Link())时。下一个, 。next将在父类中调用next对象,该对象将打印Link # 99