当我编译这段代码时:
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/