我遇到了一个接口实现的例子,我无法理解,文本中没有任何答案的推理,所以希望这里的人能帮上忙。
给定接口
interface Flyer{
void takeOff();
boolean land();
}那么,假设我有如下实现
class Aeroplane implements Flyer{
public void takeOff(){
...
}
//insert code here
return true;
}
}要插入的代码是public boolean land(){,它声明以下是不正确的boolean land(){
当接口将方法定义为public时,为什么需要使用package-private,当然boolean land(){应该实现接口,或者我遗漏了什么?
发布于 2014-03-24 14:05:45
“接口将该方法定义为包私有”
根据定义,接口中声明的所有方法都是公共的。这是无可奈何的。
这
interface Flyer{
void takeOff();
boolean land();
}与此等价
interface Flyer{
public void takeOff();
public boolean land();
}这是非法
interface Flyer{
private void takeOff();
private boolean land();
}如下所示:
interface Flyer{
protected void takeOff();
protected boolean land();
}两个人都不会编译。
发布于 2014-03-24 14:04:26
接口不将方法定义为包-私有。由接口声明的所有方法都是public。您与默认的访问修饰符混淆了。它确实是包--类是私有的,接口是public。因此,定义:
interface Flyer{
void takeOff();
void land();
}绝对等同于
interface Flyer{
public void takeOff();
public void land();
}https://stackoverflow.com/questions/22611561
复制相似问题