JAVA 引用类型和对象类型在继承中的异同

Posted jzdwajue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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









以上是关于JAVA 引用类型和对象类型在继承中的异同的主要内容,如果未能解决你的问题,请参考以下文章

接口与继承系列教材 - Java中的对象转型

Java 基础 ------- 面向对象(四) 多态

JAVA中对象创建和初始化过程

java向上转型和向下转型

基本数据类型和对象的区别

四. Java继承和多态5. instanceof 运算符