2012-04-16 08:00:19Morris
[JAVA] 學習 static and nonstatic method 的繼承
class SuperClass { public static void staticMethod() { System.out.println("Superclass's static method"); } public void instanceMethod() { System.out.println("Superclass's instance method"); } } public class Test extends SuperClass { public static void staticMethod() { System.out.println("Subclass's static method"); } public void instanceMethod() { System.out.println("Subclass's instance method"); } public static void main(String[] args) { SuperClass obj = new Test(); obj.staticMethod(); ((Test)obj).staticMethod(); obj.instanceMethod(); ((Test)obj).instanceMethod(); } }
結果:
Superclass's static method
Subclass's static method
Subclass's instance method
Subclass's instance method
被overriden的method沒有辦法再被呼叫, 除非透過override它的method, 使用super去呼叫
父別類與子類別有同名的static method....應該是稱做"hide"吧? (不知道有沒有記錯)
這種hide的情況, 透過casting就可以呼叫到不同的static method了
而override就沒辦法囉~
學習來源 :
http://www.javaworld.com.tw/jute/post/view?bid=29&id=13488&tpg=1&ppg=1&sty=1&age=0