Java 以较弱的访问权限实现接口(interface)方法时出错

标签 java interface

当我编译这段代码时:

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";
    }
}

我收到以下错误:

Camel.java:13: error: getGait() in Camel cannot implement getGait() in Rideable
String getGait() {
       ^
  attempting to assign weaker access privileges; was public
1 error

接口(interface)中声明的getGait方法如何被认为是public的?

最佳答案

在接口(interface)内声明的方法是隐式public。并且接口(interface)中声明的所有变量都是隐式public static final(常量)。

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

关于Java 以较弱的访问权限实现接口(interface)方法时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160672/

相关文章:

java - Tomcat 8.0 webapp 部署失败并出现 NullPointerException

java - 元素 "jpa"的前缀 "jpa:repositories"未绑定(bind)

java - 无法使用可运行的 jar 加载属性文件

java - 我们可以创建一个接口(interface)对象吗?

.net - 引用接口(interface)

python - 全局名称 'img' 未定义?

java - 如何使用 spring 状态机在状态转换期间抛出异常

java - 将@interface传递给 'data'类,然后显示字段 'getName()'

java - 是否可以调用 super 接口(interface)的默认方法?

java - 如何在 Oreo (8.0) 中使用隐式广播 PACKAGE_ADDED?