码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-06-17 19:35:13      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   java   ext   com   

介绍

    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 【引用类型】和【对象类型】在【继承】中的异同,布布扣,bubuko.com

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

标签:class   blog   code   java   ext   com   

原文地址:http://blog.csdn.net/three_man/article/details/31744821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!