为什么这会导致“字段名称不明确”?错误?

发布于 2025-01-08 20:36:11 字数 484 浏览 2 评论 0原文

这是代码:

public class MyClass implements Inreface1, Inreface2 {
    public MyClass() {
        System.out.println("name is :: " + name);
    }

    public static void main(String[] args) {
        new MyClass();
    }
}
//Interface1
public interface Inreface1 {
    public String name="Name";
}
 //Interface2
public interface Inreface2 {
    public String name="Name";
}

这是它导致的错误:

字段名称不明确

是什么问题?什么是暧昧?

Here is the code:

public class MyClass implements Inreface1, Inreface2 {
    public MyClass() {
        System.out.println("name is :: " + name);
    }

    public static void main(String[] args) {
        new MyClass();
    }
}
//Interface1
public interface Inreface1 {
    public String name="Name";
}
 //Interface2
public interface Inreface2 {
    public String name="Name";
}

Here is the error it causes:

The field name is ambiguous

What is the problem? What is ambiguous?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嘿看小鸭子会跑 2025-01-15 20:36:11

您的类正在实现两个接口,并且在这两个接口上都定义了变量name。因此,当您在类中调用 name 时,Java 无法确定该变量是引用 Interface1.name 还是 Interface.name

这就是你的代码的问题...

Your class is implementing two interfaces, and on both of them, the variable name is defined. Thus, when you call name in your class, Java is not able to determine if the variable refers to Interface1.name or Interface.name.

That's the problem in your code...

北风几吹夏 2025-01-15 20:36:11

MyClass 类实现了两个接口,它们都有一个 name 变量。在 MyClass 的构造函数中,Java 不知道选择哪个 name - 是 Inreface1 中的名称还是 Inreface2< 中的名称/代码>。你可以明确地告诉它:

public MyClass() {
    System.out.println("name is :: " + Inreface1.name);
}

Class MyClass implements two interfaces, which both have a name variable. In the constructor of MyClass, Java doesn't know which name to pick - the one from Inreface1 or the one from Inreface2. You could tell it explicitly:

public MyClass() {
    System.out.println("name is :: " + Inreface1.name);
}
明月夜 2025-01-15 20:36:11

看看你的代码:

System.out.println("name is :: " + name);

编译器应该使用哪个“名称”?我很模糊,因为可能是 Inreface1.name 或 Inreface2.name。
如果您通过指定一个“名称”来消除歧义,则错误应该会消失。例如:

System.out.println("name is :: " + Inreface1.name);

Look at your code:

System.out.println("name is :: " + name);

Which "name" should the compiler use? I's ambiguous, because could be Inreface1.name or Inreface2.name.
If you clear the ambiguity by specifying one "name" the error should disappear. For instance:

System.out.println("name is :: " + Inreface1.name);
家住魔仙堡 2025-01-15 20:36:11

什么是不明确的?

如果两个同名字段被接口继承
因为,例如,它的两个直接超级接口声明字段
使用该名称,就会产生一个不明确的成员。任何使用
这个不明确的成员将导致编译时错误。因此在
示例:

   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;
    }

接口LotsOfColors继承了两个名为YELLOW的字段。这是
没问题,只要接口不包含任何引用
字段 YELLOW 的简单名称。 (这样的引用可能发生在
字段的变量初始值设定项。)

即使接口PrintColors
将值 3 赋予 YELLOW 而不是值 8,
对接口 LotsOfColors 中的字段 YELLOW 的引用仍然是
被认为是不明确的。

what is ambiguous ?

If two fields with the same name are inherited by an interface
because, for example, two of its direct superinterfaces declare fields
with that name, then a single ambiguous member results. Any use of
this ambiguous member will result in a compile-time error. Thus in the
example:

   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;
    }

the interface LotsOfColors inherits two fields named YELLOW. This is
all right as long as the interface does not contain any reference by
simple name to the field YELLOW. (Such a reference could occur within
a variable initializer for a field.)

Even if interface PrintColors
were to give the value 3 to YELLOW rather than the value 8, a
reference to field YELLOW within interface LotsOfColors would still be
considered ambiguous.

☆獨立☆ 2025-01-15 20:36:11

还有一点是接口中不允许有实例变量。你的公共字符串变成一个常量:public static String name; - 你会得到两次。多个具有相同名称/类型的常量肯定是不明确的。

Another point is that instance variables are not allowed in interfaces. Your public string turns into a constant : public static String name; - which you get two times. More than one constant with the same name/type is definitely ambiguous.

美胚控场 2025-01-15 20:36:11

看来您引用的是同一个变量。

我认为编译器不知道您要传递哪个值。您是否尝试过更改字段变量?

It looks like you're referring to the same variable.

I think the compiler does not know which value you are trying to pass. Have you tried changing the field variable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文