由于非私有方法不覆盖或实现超类型的方法,因此无法使用“<>”
Cannot use '<>' due to non-private method which doesn't override or implement a method from a supertype
我有以下指定的行为。
interface ValueEditorPopupView<T> {
void setValue(T value);
void setApplyEnabled();
}
abstract class ValueEditorPopup<T> implements ValueEditorPopupView<T> {
@Override
public void setApplyEnabled() {
System.out.println("In ValueEditorPopup.setApplyEnabled()");
}
}
class ListEditorTextField {
private final ValueEditorPopup<String> popup;
ListEditorTextField() {
popup = new ValueEditorPopup<>() {
@Override
public void setValue(String value) {
System.out.println("In Anonymous Class impl setValue()");
}
// When I add a method that is NOT defined in the abstract class/interface
// I get an error for using the diamond operator in the class declaration above
// "Cannot use '<>' due to non-private method which
// doesn't override or implement a method from a supertype"
// This error clears if I explicitly specify `new ValueEditorPopup<String>(){}`
// in the anonymous class instantiation or
// add `getValues` to the abstract class/interface.
public void getValues() {}
};
}
}
如上面代码中的注释所述,当我添加一个未在摘要 class/interface 中定义的方法时,我在上面的 class 声明中使用菱形运算符时出现错误:
"Cannot use '<>' due to non-private method which doesn't override or
implement a method from a supertype"
如果我在匿名 class 实例化中显式指定 new ValueEditorPopup<String>(){}
或将 getValues
添加到摘要 class/interface.
,则此错误会清除
我不明白为什么会导致错误以及为什么添加这些 methods/types 可以修复错误。不确定我在这里遗漏了什么。
这是设计使然,在 JLS §15.9.5:
中指定
If a class instance creation expression with a ClassBody
uses a diamond (<>
) for the type arguments of the class to be instantiated, then for all non-private methods declared in the ClassBody
, it is as if the method declaration is annotated with @Override
.
您的 public getValue
方法没有覆盖 superclass 中的任何内容,因此由于隐式添加 @Override
.[=26= 而出错]
这样做的理由是:
When <>
is used, the inferred type arguments may not be as anticipated by the programmer. Consequently, the supertype of the anonymous class may not be as anticipated, and methods declared in the anonymous class may not override supertype methods as intended. Treating such methods as if annotated with @Override
(if they are not explicitly annotated with @Override
) helps avoid silently incorrect programs.
我的解释是,当你在匿名 class 中声明 public 方法时,它最有可能重写一个方法,所以编译器隐式地把 @Override
放在所有使用 <>
时的 public 方法。这是为了确保您编写的方法 do 覆盖了 superclass 中的方法,因为如果不显式写下类型参数,编译器可能会推断出意外的类型并且您编写的签名实际上并没有覆盖 superclass 中的方法。 (遗憾的是,我想不出一个很好的例子)
很少需要在不覆盖的情况下在匿名 classes 中声明 public 方法。如果你想声明一个方法,只需将它声明为私有方法——你不能从外部调用它,除非你将 new
表达式分配给 var
,这是非常罕见的。
我有以下指定的行为。
interface ValueEditorPopupView<T> {
void setValue(T value);
void setApplyEnabled();
}
abstract class ValueEditorPopup<T> implements ValueEditorPopupView<T> {
@Override
public void setApplyEnabled() {
System.out.println("In ValueEditorPopup.setApplyEnabled()");
}
}
class ListEditorTextField {
private final ValueEditorPopup<String> popup;
ListEditorTextField() {
popup = new ValueEditorPopup<>() {
@Override
public void setValue(String value) {
System.out.println("In Anonymous Class impl setValue()");
}
// When I add a method that is NOT defined in the abstract class/interface
// I get an error for using the diamond operator in the class declaration above
// "Cannot use '<>' due to non-private method which
// doesn't override or implement a method from a supertype"
// This error clears if I explicitly specify `new ValueEditorPopup<String>(){}`
// in the anonymous class instantiation or
// add `getValues` to the abstract class/interface.
public void getValues() {}
};
}
}
如上面代码中的注释所述,当我添加一个未在摘要 class/interface 中定义的方法时,我在上面的 class 声明中使用菱形运算符时出现错误:
"Cannot use '<>' due to non-private method which doesn't override or implement a method from a supertype"
如果我在匿名 class 实例化中显式指定 new ValueEditorPopup<String>(){}
或将 getValues
添加到摘要 class/interface.
我不明白为什么会导致错误以及为什么添加这些 methods/types 可以修复错误。不确定我在这里遗漏了什么。
这是设计使然,在 JLS §15.9.5:
中指定If a class instance creation expression with a
ClassBody
uses a diamond (<>
) for the type arguments of the class to be instantiated, then for all non-private methods declared in theClassBody
, it is as if the method declaration is annotated with@Override
.
您的 public getValue
方法没有覆盖 superclass 中的任何内容,因此由于隐式添加 @Override
.[=26= 而出错]
这样做的理由是:
When
<>
is used, the inferred type arguments may not be as anticipated by the programmer. Consequently, the supertype of the anonymous class may not be as anticipated, and methods declared in the anonymous class may not override supertype methods as intended. Treating such methods as if annotated with@Override
(if they are not explicitly annotated with@Override
) helps avoid silently incorrect programs.
我的解释是,当你在匿名 class 中声明 public 方法时,它最有可能重写一个方法,所以编译器隐式地把 @Override
放在所有使用 <>
时的 public 方法。这是为了确保您编写的方法 do 覆盖了 superclass 中的方法,因为如果不显式写下类型参数,编译器可能会推断出意外的类型并且您编写的签名实际上并没有覆盖 superclass 中的方法。 (遗憾的是,我想不出一个很好的例子)
很少需要在不覆盖的情况下在匿名 classes 中声明 public 方法。如果你想声明一个方法,只需将它声明为私有方法——你不能从外部调用它,除非你将 new
表达式分配给 var
,这是非常罕见的。