java继承解析

发布时间:2019-11-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java继承解析脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

A subclass inherITs all the members (fields, methods, and nested classes) From its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. [子类从其父类继承所有成员(字段,方法和嵌套类)。 构造函数不是成员,所以它们不被子类继承,但是可以从子类调用超类的构造函数。]
来自oracle官方文档https://docs.oracle.com/javas...

class Parent{     Parent() {         System.out.PRintln("调用父类构造方法!");     }     private static void staticParent() {         System.out.println("调用父类静态方法");     }     private final  void finalParent() {         System.out.println("调用父类final方法");     }     private void printParent(){         System.out.println("调用父类私有方法");     } } class Child extends Parent {     public void printChild(){         System.out.println("调用子类公有方法");     } } public class test {     public static void main(String[] args) throws Exception {         //获取子类         Class clazz = Class.forName("work.litao.Child");         //得到父类         Class suPErClass = clazz.getSuperclass();         //得到父类非继承的所以方法         Method[] methods = superClass.getDeclaredMethods();         //设置私有方法可以被访问         AccessibleObject.setAccessible(methods,true);         for (Method m:methods) {             System.out.println();             System.out.println("子类调用方法"+m.getName()+"()的调用结果:" );             m.invoke(new Child());         }      } }@H_83_126@

运行结果:

java继承解析

脚本宝典总结

以上是脚本宝典为你收集整理的java继承解析全部内容,希望文章能够帮你解决java继承解析所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。