我有下面指定的行为.
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() {}
};
}
}
正如上面代码中的一条注释所述,当我添加一个抽象类/接口中未定义的方法时,我在上面的类声明中使用菱形运算符时会出现一个错误:
"无法使用"<;>;",因为非私有方法不重写或
如果我在匿名类实例化中显式指定new ValueEditorPopup<String>(){}
,或者在抽象类/接口中添加getValues
,那么这个错误就会清除.
我不明白为什么会出现这个错误,为什么添加这些方法/类型可以修复它.我不确定我错过了什么.