简体   繁体   English

接口模糊地继承了字段

[英]Interface ambiguously inherited fields

I am going through JLS section 9.3.1 and i came across an interesting concept of ambigious inherited fields. 我正在通过JLS第9.3.1节,我遇到了一个有趣的继承领域的概念。 This is the example from JLS 这是JLS的示例

interface BaseColors {
int RED = 1, GREEN = 2, BLUE = 4;
}
interface RainbowColors extends BaseColors {
int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7;
}
interface PrintColors extends BaseColors {
int YELLOW = 8, CYAN = 16, MAGENTA = 32;
}
interface LotsOfColors extends RainbowColors, PrintColors {
int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED+90;
}

It is allowing to have ambiguous fields inherited. 它允许继承模糊字段。 But when i try to reference the field and access it, it gives compile time error. 但是当我尝试引用该字段并访问它时,它会产生编译时错误。 Giving a compile time error for ambigious fields. 为ambigious字段提供编译时错误。 My question is, at first point why the compiler didn't complain when the ambigious field was inherited. 我的问题是,第一点为什么编译器在继承了这个野蛮的领域时没有抱怨。 Why at access time, it's giving this issue? 为什么在访问时,它会给出这个问题? If we do the same when using classes., It allows. 如果我们在使用类时也这样做,它允许。 Why not in case of interfaces. 为什么不在接口的情况下。 My point is it shouldn't allow at the first instant only. 我的观点是它不应该只在第一时刻允许。 Clarification on this concept will be pretty helpful. 澄清这个概念将非常有帮助。

Interface fields are implicitely static final. 接口字段是隐含的最终静态。 And static fields are never inherited. 静态字段永远不会被继承。 You can hide a field by defining a new field with the same name, but you just need to qualify the field name with the appropriate interface to resolve the conflict: 您可以通过定义具有相同名称的新字段来隐藏字段,但您只需要使用适当的接口限定字段名称以解决冲突:

PrintColors.YELLOW

or 要么

RainbowCOlors.YELLOW

EDIT: 编辑:

To clarify (hopefully): 澄清(希望):

The compiler allows you to use LotsOfColors.MAGENTA in the source code, although the field is actually defined in PrintColors.MAGENTA . 编译器允许您在源代码中使用LotsOfColors.MAGENTA ,尽管该字段实际上是在PrintColors.MAGENTA定义的。 But that's only to make your life a bit easier, especially when you reference a field from a superclass in a subclass. 但这只是为了让您的生活更轻松,尤其是当您从子类中的超类引用字段时。

In the byte-code, though, the compiler replaces the reference to LotsOfColors.MAGENTA by a reference to PrintColors.MAGENTA . 在字节码,虽然,编译器代替了参考LotsOfColors.MAGENTA通过向参考PrintColors.MAGENTA It all happens at compilation time, and not at runtime like for polymorphic methods. 这一切都发生在编译时,而不是在运行时,就像多态方法一样。

When you have an ambiguity (like for LotsOfColors.YELLOW ), the compiler can't decide which of the fields you actually want to use. 当你有歧义时(比如LotsOfColors.YELLOW ),编译器无法决定你真正想要使用哪个字段。 It can be PrintColors.YELLOW or RainbowColors.YELLOW . 它可以是PrintColors.YELLOWRainbowColors.YELLOW So, instead of taking an arbitrary decision, the compiler produces a compilation error, to force you to resolve the ambiguity. 因此,编译器不会做出任意决定,而是会产生编译错误,迫使您解决歧义。 And you resolve the ambiguity in the source code by providing the actual class name, either PrintColors.YELLOW or RainbowColors.YELLOW . 并且您通过提供实际的类名称PrintColors.YELLOWRainbowColors.YELLOW解决源代码中的歧义。

接口中的字段默认为public static final因此它们不会被继承

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM