Akh*_*yan 1 java scope interface access-specifier
interface Rideable {
String getGait();
}
public class Camel implements Rideable {
int weight = 2;
String getGait() {
return " mph, lope";
}
void go(int speed) {++speed;
weight++;
int walkrate = speed * weight;
System.out.print(walkrate + getGait());
}
public static void main(String[] args) {
new Camel().go(8);
}
}
Run Code Online (Sandbox Code Playgroud)
编译上面的代码后,我得到了一个编译错误,与getGait()方法的访问修饰符有关.请解释一下,为什么我要getGait()使用public访问修饰符声明?
getGait()的Camel实现的方法Rideable接口.public默认情况下,所有接口方法都是默认的(即使您未在接口定义中明确指定它),因此所有实现方法也必须是公共的,因为您无法降低接口方法的可见性.
| 归档时间: |
|
| 查看次数: |
1079 次 |
| 最近记录: |