我知道并完全同意通过接口共享常量被认为是一种不好的做法,但我没有选择这种实现。任何:
interface SuperClassConstants {
public static final String CONSTANT = "super";
}
interface SubClassConstants extends SuperClassConstants {
public static final String CONSTANT = "sub";
}
class SuperClass implements SuperClassConstants {
}
class SubClass extends SuperClass implements SubClassConstants {
public SubClass() {
System.out.println(CONSTANT);
// The field CONSTANT is ambiguous
}
}
为什么模棱两可?常数不是隐藏的吗?我以为 Java 会理解我所期待的"sub"
......