编译失败

时间:2012-12-02 17:46:07

标签: java interface compiler-errors

这是一段Java代码:

interface Rideable {
    String getGait();
}

public class Camel implements Rideable {
    int x = 2;
    public static void main(String[] args) {
        new Camel().go(8);
    }

    void go(int speed) {
        System.out.println((++speed * x++) + this.getGait());
    }

    String getGait() {
        return " mph, lope";
    }
}

事实证明编译失败(根据Oracle),但在我看来,它应该运行良好,产生输出。那么,编译失败的罪魁祸首在哪里? 干杯

2 个答案:

答案 0 :(得分:6)

您无法降低覆盖方法的可见性(默认情况下界面方法为public),请尝试以下方法:

public String getGait() {
    return " mph, lope";
}

答案 1 :(得分:2)

您已使用默认访问权限定义getGait,但接口定义要求其实现为public

public String getGait() {