我是 Java 新手,正在尝试学习继承的概念。当我尝试使用主类的 identifyMyself 方法()调用 EggLayer 的 identifyMyself()方法时
System.out.println(EggLayer.super.identifyMyself());
它按预期工作。但是,当我尝试使用相同的语句从主类的 main method() 调用 EggLayer 的 identifyMyself() 方法时,编译器会生成错误消息:“不是封闭类:EggLayer”。
有人可以向我解释为什么会这样吗?
interface Animal {
default public String identifyMyself() {
return "I am an animal.";
}
}
interface EggLayer extends Animal {
default public String identifyMyself() {
return "I am able to lay eggs.";
}
}
interface FireBreather extends Animal {
@Override
default public String identifyMyself(){
return "I'm a firebreathing animal";
}
}
public class Dragon implements EggLayer, FireBreather {
public static void main (String... args) {
Dragon myApp = new Dragon();
System.out.println(myApp.identifyMyself());
/**
*Not allowed, compiler says "not a enclosing class: EggLayer"
*System.out.println(EggLayer.super.identifyMyself());
*/
}
public String identifyMyself(){
//Call to EggLayer.super.identifyMyself() allowed
System.out.println(EggLayer.super.identifyMyself());
return "im a dragon egglayer firebreather";
}
}