理解抽象的超级关键字 类
Understanding super keyword for abstract classes
考虑以下 class:
public abstract class AbstractClass {
public abstract String m();
public AbstractClass get(){
return new AbstractClass() {
@Override
public String m() {
return "Anonymous " + super.m(); //1, Compile-time erro
}
};
}
}
不清楚为什么禁止这样使用 super
。在//1
,出现如下错误
Cannot directly invoke the abstract method m() for the type AbstractClass
所以,我查阅了JLS 15.11.2并没有找到阻止此类代码被编译的限制。他们在这里:
- 显然
It is a compile-time error if the forms using the keyword super appear
in the declaration of class Object, since Object has no superclass.
- 因为不可能有
AbstractClass
的实例,但只有一个 concrete subclasses,以下内容在我看来也有效:
The forms using the keyword super are valid only in an instance
method, instance initializer, or constructor, or in the initializer of
an instance variable of a class. If they appear anywhere else, a
compile-time error occurs.
- 并非如此。
It is a compile-time error if the current class is not an inner class
of class T or T itself.
当然,我可以使用AbstractClass.this.m()
,但这不是我要问的。
据我所知,编译器在使用 super.m();
时会尝试使用静态绑定。由于没有方法 super.m();
因为它是抽象的编译器已经在编译时抱怨了。
Java 中的静态绑定意味着方法在编译时解析,而动态绑定发生在运行时,当使用可以被多个子类覆盖的方法时。
super 关键字在这里不起作用,因为 AbstractClass.m()
已被声明为抽象的,因此在内部 class 的父级上没有合适的实现。请记住,内部 classes 不会扩展外部 class(即使它属于同一类型),它们包含对它的引用。
然而,当从内部 class 调用外部 class 时(我相信你打算在这里做的)然后使用以下语法 AbstractClass.this.m()
.
以下将按预期编译和工作。
public abstract class AbstractClass {
public abstract String m();
public AbstractClass get(){
return new AbstractClass() {
@Override
public String m() {
return "Anonymous " + AbstractClass.this.m();
}
};
}
}
考虑以下 class:
public abstract class AbstractClass {
public abstract String m();
public AbstractClass get(){
return new AbstractClass() {
@Override
public String m() {
return "Anonymous " + super.m(); //1, Compile-time erro
}
};
}
}
不清楚为什么禁止这样使用 super
。在//1
,出现如下错误
Cannot directly invoke the abstract method m() for the type AbstractClass
所以,我查阅了JLS 15.11.2并没有找到阻止此类代码被编译的限制。他们在这里:
- 显然
It is a compile-time error if the forms using the keyword super appear in the declaration of class Object, since Object has no superclass.
- 因为不可能有
AbstractClass
的实例,但只有一个 concrete subclasses,以下内容在我看来也有效:
The forms using the keyword super are valid only in an instance method, instance initializer, or constructor, or in the initializer of an instance variable of a class. If they appear anywhere else, a compile-time error occurs.
- 并非如此。
It is a compile-time error if the current class is not an inner class of class T or T itself.
当然,我可以使用AbstractClass.this.m()
,但这不是我要问的。
据我所知,编译器在使用 super.m();
时会尝试使用静态绑定。由于没有方法 super.m();
因为它是抽象的编译器已经在编译时抱怨了。
Java 中的静态绑定意味着方法在编译时解析,而动态绑定发生在运行时,当使用可以被多个子类覆盖的方法时。
super 关键字在这里不起作用,因为 AbstractClass.m()
已被声明为抽象的,因此在内部 class 的父级上没有合适的实现。请记住,内部 classes 不会扩展外部 class(即使它属于同一类型),它们包含对它的引用。
然而,当从内部 class 调用外部 class 时(我相信你打算在这里做的)然后使用以下语法 AbstractClass.this.m()
.
以下将按预期编译和工作。
public abstract class AbstractClass {
public abstract String m();
public AbstractClass get(){
return new AbstractClass() {
@Override
public String m() {
return "Anonymous " + AbstractClass.this.m();
}
};
}
}