在讨论过程中,我的一位朋友告诉我,concrete methods would be allowed in java 1.8 in interfaces当时我的脑海里浮现出一个问题,即如果他们被允许那么我们将如何区分这些方法.例如
我有两个接口Animal.java,Pet.java并且两者都有相同的具体方法,即eat()
public interfaces Animal{
void eat(){
System.out.println("Animal Start eating ....");
}
}
public interfaces Pet{
void eat(){
System.out.println("Pet Start eating ....");
}
}
Run Code Online (Sandbox Code Playgroud)
现在我Zoo.java实现这两个并没有覆盖
public class Zoo() implements Pet , Animal{
//Now name method is a part of this class
}
Run Code Online (Sandbox Code Playgroud)
现在这是我的困惑.如何animal使用Testobject 调用inteface上的特定方法
public class Demo{
public static void main(String[] args){
Zoo zoo = new Zoo();
zoo.eat(); //What would be the output
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?或者在java1.8中是否有任何解决方案,因为我无法找到它的答案.
除非在Zoo类中重写eat,否则会出现编译时错误.
java: class defaultMethods.Zoo inherits unrelated defaults for eat() from types Pet and Animal
Run Code Online (Sandbox Code Playgroud)
最新和最好的jdk在这里顺便说一下.语法应该是
default void eat(){
System.out.println("Animal Start eating ....");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12464 次 |
| 最近记录: |