关于覆盖和隐藏的问题

scuthdd 2003-09-09 10:01:28
public class planet{
public static void hide(){
System.out.println("The hide method in Planet.");
}
public void override(){
System.out.println("The override method in Planet.");
}
}

public class earth extends planet{
public static void hide(){
System.out.println("The hide method in Earth.");
}
public void override(){
System.out.println("The override method in Earth.");
}
pubilc ststic void main (String[] args){
Earth myEarth=new Earth();
Planet myPlanet=(Planet)myEarth;
myPlanet.hide();
myPlanet.override();
}
}


输出
the hide method in Planet.
The override method in Earth.

是怎么得来的,哪一句是控制输出的?

myPlanet.hide();
myPlanet.override();
这两句能说说什么意思嘛?
...全文
50 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaBoyCaoJi 2003-09-10
  • 打赏
  • 举报
回复
static方法不能覆盖,private方法也不能覆盖。Java视它们为被隐藏.
duoshanx 2003-09-10
  • 打赏
  • 举报
回复
第一版有笔误,见第二版。
duoshanx 2003-09-10
  • 打赏
  • 举报
回复
static方法不能覆盖,private方法也不能覆盖。Java视它们为被隐藏.

在Planet里hide()是static,所以当earth 也有hide()时,Java视Planet里hide()被隐藏.
所以myPlanet.hide();是Planet里的。

因为Planet里的override()不是static,也不是private,所以当earth 也有override()时,
这是重载,不会理会Planet里的override()了。myPlanet.override()就是earth里的.

当使用 Planet myPlanet=(Planet)myEarth;时,一定是使用的Planet里的static方法。
当使用 Earth myEarth=new Earth();   时,如果myEarth里有override();一定不会用Planet里的override(); 方法。

当使用Earth myEarth=new Earth();一定是使用的Earth里的static方法,因为Java视static也就是hide()为被隐藏.不会再用了。


说完了,自己也晕了。
不知道说明白没有。

duoshanx 2003-09-10
  • 打赏
  • 举报
回复
static方法不能覆盖,private方法也不能覆盖。Java视它们为被隐藏.

在Planet里hide()是static,所以当earth 也有hide()时,Java视Planet里hide()被隐藏.
所以myPlanet.hide();是Planet里的。

因为Planet里的override()不是static,也不是private,所以当earth 也有override()时,
这是重载,不会理会Planet里的override()了。myPlanet.override()就是earth里的.

当使用 Planet myPlanet=(Planet)myEarth;时,一定是使用的Planet里的static方法。
当使用 Planet myPlanet=(Planet)myEarth;时,如果myEarth里有override();一定不会用Planet里的override(); 方法。

当使用Earth myEarth=new Earth();一定是使用的Earth里的static方法,因为Java视static也就是hide()为被隐藏.不会再用了。


说完了,自己也晕了。
不知道说明白没有。




62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧