首页 > 代码库 > JAVA 【引用类型】和【对象类型】在【继承】中的异同

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