相关疑难解决方法(0)

使用相同的方法在类中实现两个接口.覆盖哪种接口方法?

两个具有相同方法名称和签名的接口.但是由单个类实现,那么编译器将如何识别哪个接口的方法是什么?

例如:

interface A{
  int f();
}

interface B{
  int f();
}

class Test implements A, B{   
  public static void main(String... args) throws Exception{   

  }

  @Override
  public int f() {  // from which interface A or B
    return 0;
  }
}   
Run Code Online (Sandbox Code Playgroud)

java overriding interface

223
推荐指数
6
解决办法
14万
查看次数

界面是否解决了"致命的死亡钻石"问题?

界面是否解决了死亡问题的致命钻石

我不这么认为,例如:

// A class implementing two interfaces Interface1 and Interface2.
// Interface1 has int x=10 and Interface2 has int x = 20

public class MultipleInterface implements Interface1, Interface2{

    public void getX(){
        System.out.println(x);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我们得到一个模棱两可的x.

虽然接口是解决方法模糊性的好方法,但我猜它们在变量的情况下会失败?

我对么?如果我遗失了什么,请赐教.

java interface multiple-inheritance

5
推荐指数
4
解决办法
1万
查看次数