java - java中的多个接口(interface)具有相同的方法名称和变量名称

标签 java oop interface

接口(interface) 1 具有方法 1、变量 x,接口(interface) 2 具有方法 1、变量 x。 为什么在第 1 行显示错误,而在第 2 行却没有显示错误?

interface interface1{
    public int x =10;
    public void method1();
}
interface interface2{
    public int x =11;
    public void method1();
}

public class Test implements interface1, interface2{

    int y = x; // Line 1
    @Override
    public void method1() {  //Line 2
    }

}

最佳答案

“x”不明确,因为作用域中有两个,每个接口(interface)都有一个。相比之下,“method1()”则不然,因为根据 Java 规则,Test 中的定义满足提供两个接口(interface)中定义的实现的要求。

关于java - java中的多个接口(interface)具有相同的方法名称和变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14290902/

相关文章:

javascript - oo javascript 具有来自服务器的属性、来自缓存的方法,最佳实践?

c# - 在 WCF 中返回许多接口(interface)

c# - SQLite .NET 是否在 CreateTable 方法中接受接口(interface)

c# - 如何序列化接口(interface)默认属性?

java - 在 java 1.8 中将 Map<String, List<Object>> 转换为 Map<String, List<String>>

java - 如何创建一个自定义异常,它在 java 中包装多个异常

javascript - 如何创建基于以 "_"开头的属性名称将所有类属性设置为不可枚举的 Typescript 装饰器?

java - 错误 : 'SDL init failure, reason is: No available video device' when launching AVD

java - Android 中的重叠按钮

oop - 练习OOP设计技能的网站