小编Jal*_*eri的帖子

接口和继承:"返回类型int不兼容"

public interface MyInterface{
    public int myMethod();
}


public class SuperClass {
    public String myMethod(){
        return "Super Class";
    }
}

public class DerivedClass extends SuperClass implements MyInterface {
    public String myMethod() {...}  // this line doesn't compile 
    public int myMethod() {...}     // this is also unable to compile
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,DerivedClass它给了我错误

java: myMethod() in interfaceRnD.DerivedClass cannot override myMethod() in interfaceRnD.SuperClass
  return type int is not compatible with java.lang.String

我该如何解决这个问题?

java inheritance interface

19
推荐指数
2
解决办法
2582
查看次数

标签 统计

inheritance ×1

interface ×1

java ×1