子类继承父类的static方法

<pre name="code" class="java">package testBaidu;

public class Fruit {

	static String color = "黄色";
	String size = "大";
	
	static String getFruitColor() {
		return color;
	}
	public String getFruitSize() {
		return size;
	}
}



</pre><pre name="code" class="java">package testBaidu;

public class Apple extends Fruit{
	static String appleColor = "绿色";
	String appleSize = "小";
	
	static String getFruitColor() {
		return appleColor;
	}
	
	public String getFruitSize() {
		return appleSize;
	}
	
	public static void main(String[] args) {
		Fruit apple = new Apple();
		System.out.println(apple.getFruitColor());
		System.out.println(apple.getFruitSize());
		
	}
	
}

打印结果:

黄色


子类继承父类,重写父类的静态方法无效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章