I've seen a couple of examples similar to this in Java, and am hoping someone can explain what is happening. It seems like a new class can be defined inline, which seems really weird to me.
The first printout line is expected, since it is simply the toString. However the 2nd seems like the function can be overriden inline.
Is there a technical term for this?
Or any documentation which goes into more depth?
If I have the following code:
public class Apple {
public String toString() {
return "original apple";
}
}
public class Driver {
public static void main(String[] args) {
System.out.println("first: " + new Apple());
System.out.println("second: " +
new Apple() {
public String toString() {
return "modified apple";
}
}
);
}
}
The code outputs:
first: original apple
second: modified apple
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…