static method

public class static_method {
	public static void main(String[] args) {
		Super2 sup2 = new Sub2();
		/**
		 * output: class com.private_field_static_pitfall.test.Sub2
		 */
		System.out.println(sup2.getClass());
		/**
		 * output: super2 f()靜態方法不存在多態調用
		 */
		sup2.f();
	}
}
class Super2 {
	public static void f() {
		System.out.println("super2 f()");
	}
}
class Sub2 extends Super2 {
	public static void f() {
		System.out.println("sub2 f()");
	}
	/**
	 * error:This instance method cannot override the static method from Super2
	protected void f() {
		
	}
	 */
}
相關文章
相關標籤/搜索