java 子类父类关于静态方法调用
public class Cat
{
protected static String color = "random";
public Cat() { }
public void showCatColor()
{
System.out.println("Cat:" + color);
}
public static void showColor()
{
System.out.println("Cat:" + color);
}
}
public class WhiteCat extends Cat
{
protected static String color = "white";
public WhiteCat()
{
super();
}
public void showCatColor()
{
System.out.println("WhiteCat:" + color);
}
public static void showColor()
{
System.out.println("WhiteCat:" + color);
}
}
WhiteCat whiteCat = new WhiteCat ;
Cat cat = whiteCat ;
cat.showColor();
cat.showCatColor();
执行结果为:Cat:random
WhiteCat:white
为什么cat.showColor()的执行结果是Cat:random,二不是WhiteCat:white
------解决方案--------------------
因为静态方法没有多态的特性。。