閱讀798 返回首頁    go 阿裏雲 go 技術社區[雲棲]


JAVA 【引用類型】和【對象類型】在【繼承】中的異同

介紹

    JAVA 【引用類型】和【對象類型】在【繼承】中的異同,這個問題自己整理過N次,也被人當菜鳥問過N次。所以,在此簡單整理一下,以供大家分享。

  • 在繼承關係中,一般成員變量是根據引用類型
  • 在繼承關係中,靜態成員變量是根據引用類型
  • 在繼承關係中,一般方法是根據對象類型
  • 在繼承關係中,靜態方法是根據引用類型

注意

    靜態成員變量,靜態方法是基於類的,本文為了測試觀察,所以,會用對象去引用靜態成員變量和靜態方法。

    


Super Class:

package shuai.study.inherit;

public class SuperClass {
	public String commonString = "SuperClass Common String";
	public static String staticString = "SuperClass Static String";

	public int otherCommonInt = 0;
	public static int otherStaticInt = 10;

	public void commonMethod() {
		System.out.println("SuperClass Common Method: " + commonString);
	}

	public static void staticMethod() {
		System.out.println("SuperClass Static Method: " + staticString);
	}

	public void otherCommonMethod() {
		System.out.println("SuperClass Other Common Method: " + otherCommonInt);
	}

	public static void otherStaticMethod() {
		System.out.println("SuperClass Other Static Method: " + otherStaticInt);
	}
}

Suber Class:  
package shuai.study.inherit;

public class SuberClass extends SuperClass {
	public String commonString = "SuberClass Common String";
	public static String staticString = "SuberClass Static String";

	@Override
	public void commonMethod() {
		System.out.println("SuberClass Common Method: " + commonString);
	}

	public static void staticMethod() {
		System.out.println("SuberClass Static Method: " + staticString);
	}
}

Test Class:

package shuai.study.inherit;

public class Test {

	public static void main(String[] args) {
		SuperClass superClass = new SuberClass();

		// Common member variable is according to quote type
		System.out.println(superClass.commonString);

		// Static member variable is according to quote type
		// Generally we invoke static member as SuperClass.staticString, because static method & variable is based on class
		System.out.println(superClass.staticString);

		// Common method is according to object type
		superClass.commonMethod();

		// Static method is according to quote type
		// Generally we invoke static method as SuperClass.staticMethod(), because static method & variable is based on class
		superClass.staticMethod();

		System.out.println("==================================================");

		SuberClass suberClass = new SuberClass();

		// Inherit member variable from super class
		System.out.println(suberClass.otherCommonInt);
		System.out.println(SuberClass.otherStaticInt);

		// Inherit method from super class
		suberClass.otherCommonMethod();
		SuberClass.otherStaticMethod();

		// Self method
		suberClass.commonMethod();
		SuberClass.staticMethod();
	}

}

Output:

SuperClass Common String
SuperClass Static String
SuberClass Common Method: SuberClass Common String
SuperClass Static Method: SuperClass Static String
==================================================
0
10
SuperClass Other Common Method: 0
SuperClass Other Static Method: 10
SuberClass Common Method: SuberClass Common String
SuberClass Static Method: SuberClass Static String


最後更新:2017-04-03 07:57:07

  上一篇:go hi3531 SDK 編譯 kernel, 修改 參數 .
  下一篇:go ORACLE union order by