2

这是一段 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),但在我看来,它应该可以正常运行并产生输出。那么,编译失败的罪魁祸首在哪里呢?干杯

4

2 回答 2

6

You can't reduce the visibility of methods you override (interface methods are public by default), try this instead:

public String getGait() {
    return " mph, lope";
}
于 2012-12-02T17:49:20.033 回答
2

You have defined getGait with default access but interface definitions are require their implementations to be public.

public String getGait() {
于 2012-12-02T17:49:24.273 回答