2008-01-30

一个细节

关键字: java
今天做一面试题,题意中代码如下:
package test;

class Cat {
    private static String color="donot know";
    public void showColor(){
       System.out.println("cat:  "+color);
      }
    public static void showStaColor(){
       System.out.println("cat:  "+color);
      }
}

class WhriteCat extends Cat{
    private static String color1="whrite";
    public void showColor(){
       System.out.println("whitecat:  "+color1);
      }
    public static void showStaColor(){
       System.out.println("whitecat:  "+color1);
      }

}

public class Test {
	public static void main(String[] args) {
		Cat wc=(Cat)(new WhriteCat());
		wc.showColor();
		wc.showStaColor();		
	}
}


请写出以上代码的结果:

知道的可回一下。
评论
jj_sof 2008-08-21
应该是考 先静态 然后 非静态那个~~ 忘记了
igogo007 2008-08-20
whitecat: whrite
cat: donot know



public static void main(java.lang.String[]);
Code:
Stack=2, Locals=2, Args_size=1
0: new #2; //class WhriteCat
3: dup
4: invokespecial #3; //Method WhriteCat."<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4; //Method Cat.showColor:()V
12: aload_1
13: pop
14: invokestatic #5; //Method Cat.showStaColor:()V
17: return
zhongw 2008-08-20
这样的话,把
Cat wc=(Cat)(new WhriteCat()); 改成: Cat wc=new WhriteCat();

结果是一样的了。那何必要类型转换啊?
rbible 2008-08-19
这个应用还是很基础的

类的多态和类的静态成员

非静态方法的取决于对象

静态的方法取决于引用类,与对象无关
billy1977 2008-01-30
想想没有那么简单,我测试了一下,其实是:
whitecat: whrite
cat: donot know
想了一下想明白了,静态函数与类关联,与对象无关。
billy1977 2008-01-30
whitecat:whrite
whitecat:whrite
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

rbible
搜索本博客
我的相册
Aa5b5e14-33d2-37be-9f5f-9f8e5edc8682-thumb
迎迎.bmp
共 5 张
存档
最新评论