Topic: 求助:关于覆盖和隐藏的问题(代码分析)

  Print this page

1.求助:关于覆盖和隐藏的问题(代码分析) Copy to clipboard
Posted by: 彩虹
Posted on: 2004-11-17 18:46

public class Planet{
public static void hide(){
System.out.println("The hide method in Planet.");
}
public void override(){
System.out.println("The overrid 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.");
}
public static 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.
问题:为什么
输出是 The override method in Earth.
而不是 The override method in Planet.

2.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-17 19:43

你的程序根本执行不了呀,还有好多错误呢,你的执行结果是怎么来的呢(是书上的例子什么的吗?)
首先,正确的代码是

public class Planet{
public static void hide(){
System.out.println("The hide method in Planet.");
}
public void override(){
System.out.println("The overrid method in Planet.");
}

//----------------------------------------------------------------------
public static void main(String[] args){
Earth myEarth=new Earth();
Planet myPlanet=(Planet)myEarth;
myPlanet.hide();
myPlanet.override();
}
}
//-------------------------------------------
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.");
}
}

3.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: dorrenchen
Posted on: 2004-11-17 22:53

static method can not be overrided, but you can hide the super class static method with the same signature.

see http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

since you cast myEarth to Planet type (myPlanet), so when you call myPlanet.hide(), Planet's static hide() method will be called. However, if you call myEarth.hide(), you'll get "The hide method in Earth.", myEarth.hide() is hiding the Planet.hide(), as I mentioned in 1st sentence.

4.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: magin
Posted on: 2004-11-18 13:58

因为myPlanet并不是对象的实例,只是指向myEarth的一个引用。所以,楼主的问题就解答了。

如果要问为什么是
The hide method in Planet.而不是The hide method in Earth.
那么也许DorrenChen说的对,静态方法不能重载。

5.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: HJILY44
Posted on: 2004-11-18 14:45

静态方法不能被重置,不是重载!!!!

6.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: magin
Posted on: 2004-11-19 15:50

上面的意思是静态方法可以被重载咯?

又问重置是什么意思呀?

7.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: 彩虹
Posted on: 2004-11-23 08:45

还有书上说:"myPlanet的编译时类型是planet.运行时系统调用planet中定义的hide
方法."那编译是什么样的,运行又是什么样子呢?如何才能看到.

8.Re:求助:关于覆盖和隐藏的问题(代码分析) [Re: 彩虹] Copy to clipboard
Posted by: ninky
Posted on: 2004-11-29 09:17

为什么 把
class Earth extends Planet的那段代码放在main 之前 就会报错那


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923