I am a amateur programmer and I recently stumbled upon this special way of overriding a function when studying abstract classes: (我是一名业余程序员,最近我在研究抽象类时偶然发现了一种覆盖函数的特殊方法:)
abstract class Test {
abstract String message();
}
public class Abstract {
public static void main(String[] args) {
String word = new Test() {
String message() {
return "Hello World";
}
}.message();
}
}
I am confused at specific this part in the code: (我对代码中的这一部分感到困惑:)
String word = new Test() {
String message() {
return "Hello World";
}
}.message();
From looking at this specific section of the code, it seems that the abstract class is being "instantiated" (if I am using the term correctly). (从代码的这一特定部分来看,似乎抽象类正在“实例化”(如果我正确使用了该术语)。) However, I learned that abstract classes cannot be instantiated. (但是,我了解到不能实例化抽象类。)
I know this is quite of an obvious question, is it possible if you can explain to me what is happening when that part of the code is being complied? (我知道这是一个很明显的问题,是否可以向我解释在编译那部分代码时发生了什么?) I would be really grateful if you do so. (如果您这样做,我将不胜感激。)
Thank you, (谢谢,)
ask by Simeon Tran translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…