首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。