子類繼承父類的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());
		
	}
	
}

打印結果:

黃色


子類繼承父類,重寫父類的靜態方法無效。

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