• 普通类、抽象类、接口之间的继承实现关系


    普通类、抽象类、接口之间的继承实现关系图(参考:类与抽象类接口的关系):

    下面从执行的结果中,清晰呈现了代码的执行顺序。序号 5~9 说的问题,哪位能明白是咋会儿事的,请在评论区指导一下,谢谢! 

     1 package test;
     2 
     3 public class ExtentsAndImplements {
     4 
     5     /**
     6      * 这块代码打开注释,FatherRootClass()、FatherRootAbstract()、ChildrenClass()
     7      * 上面这一串构造函数会在 System.out.println(" call Main() ................") 与 主类构造函数 ExtentsAndImplements() 之间执行【比较不好理解】。
     8      **/
     9 //     ChildrenClass childrenClass = new ChildrenClass();
    10 
    11     ExtentsAndImplements (){
    12         System.out.println(" 主类无参构造函数执行:ExtentsAndImplements construct 11111111111111111111");
    13     }
    14 
    15     public static void main(String[] args) {
    16         System.out.println(" call Main() ................");
    17         ChildrenClass childrenClass = new ExtentsAndImplements().new ChildrenClass(" new ChildrenClass(param) : ");
    18         System.out.println(" ChildrenClass test() ................");
    19         childrenClass.test();
    20     }
    21 
    22     class FatherRootClass {
    23         private FatherRootClass(){
    24             System.out.println(" FatherRootClass No construct !!!=================");
    25         }
    26         private FatherRootClass(String FatherRootClass){
    27             System.out.println(FatherRootClass + " FatherRootClass construct !!!=================");
    28         }
    29     }
    30 
    31     interface SimpleRootInterface {
    32         void run();
    33     }
    34 
    35     abstract class FatherRootAbstract extends FatherRootClass implements SimpleRootInterface{
    36         public FatherRootAbstract(){
    37             System.out.println(" FatherRootAbstract extends FatherRootClass No construct !!!  #######################");
    38         }
    39         public FatherRootAbstract(String FatherRootAbstract){
    40             super(FatherRootAbstract);
    41             System.out.println(FatherRootAbstract + " FatherRootAbstract extends FatherRootClass construct !!!  #######################");
    42         }
    43 
    44         abstract void excute();
    45 
    46         @Override
    47         public void run() {
    48             this.excute();
    49             System.out.println(" FatherRootAbstract implements SimpleRootInterface.run(). " +
    50                     "this is FatherRootAbstract'run(). ------333333333333333333333");
    51         }
    52 
    53     }
    54 
    55     class ChildrenClass extends FatherRootAbstract{
    56         public ChildrenClass(){
    57             System.out.println(" ChildrenClass extends FatherRootAbstract No construct !!!  @@@@@@@@@@@@@@@@@@@@");
    58         }
    59         public ChildrenClass(String begin){
    60             super(begin);
    61             System.out.println(begin + " ChildrenClass extends FatherRootAbstract construct !!!  @@@@@@@@@@@@@@@@@@@@");
    62         }
    63 
    64         private void test(){
    65 //            super.run();
    66             this.run();
    67             this.excute();
    68             System.out.println(" SUCCESS!");
    69         }
    70 
    71         @Override
    72         public void excute() {
    73             System.out.println(" ChildrenClass extends FatherRootAbstract implements SimpleRootInterface. " +
    74                     "this is ChildrenClass'excute(). ------666666666666666666666");
    75         }
    76     }
    77 
    78 }

    运行结果:

     1  call Main() ................
     2  主类无参构造函数执行:ExtentsAndImplements construct 11111111111111111111
     3  new ChildrenClass(param) :  FatherRootClass construct !!!=================
     4  new ChildrenClass(param) :  FatherRootAbstract extends FatherRootClass construct !!!  #######################
     5  new ChildrenClass(param) :  ChildrenClass extends FatherRootAbstract construct !!!  @@@@@@@@@@@@@@@@@@@@
     6  ChildrenClass test() ................
     7  ChildrenClass extends FatherRootAbstract implements SimpleRootInterface. this is ChildrenClass'excute(). ------666666666666666666666
     8  FatherRootAbstract implements SimpleRootInterface.run(). this is FatherRootAbstract'run(). ------333333333333333333333
     9  ChildrenClass extends FatherRootAbstract implements SimpleRootInterface. this is ChildrenClass'excute(). ------666666666666666666666
    10  SUCCESS!

    其余代码不变,把序号9 (ChildrenClass childrenClass = new ChildrenClass();)的注释打开,再执行程序,运行结果为(结果中的2、3、4 在1、5之间执行,我有点懵,有能看明白的高手,请在评论区点化点化(* ̄︶ ̄) ):

     1  call Main() ................
     2  FatherRootClass No construct !!!=================
     3  FatherRootAbstract extends FatherRootClass No construct !!!  #######################
     4  ChildrenClass extends FatherRootAbstract No construct !!!  @@@@@@@@@@@@@@@@@@@@
     5  主类无参构造函数执行:ExtentsAndImplements construct 11111111111111111111
     6  new ChildrenClass(param) :  FatherRootClass construct !!!=================
     7  new ChildrenClass(param) :  FatherRootAbstract extends FatherRootClass construct !!!  #######################
     8  new ChildrenClass(param) :  ChildrenClass extends FatherRootAbstract construct !!!  @@@@@@@@@@@@@@@@@@@@
     9  ChildrenClass test() ................
    10  ChildrenClass extends FatherRootAbstract implements SimpleRootInterface. this is ChildrenClass'excute(). ------666666666666666666666
    11  FatherRootAbstract implements SimpleRootInterface.run(). this is FatherRootAbstract'run(). ------333333333333333333333
    12  ChildrenClass extends FatherRootAbstract implements SimpleRootInterface. this is ChildrenClass'excute(). ------666666666666666666666
    13  SUCCESS!
  • 相关阅读:
    spring boot 1.4 整合 mybatis druid
    大话设计模式读书笔记--6个原则
    大话设计模式读书笔记--23.访问者模式
    大话设计模式读书笔记--22.解释器模式
    大话设计模式读书笔记--21.享元模式
    大话设计模式读书笔记--20.中介者模式
    大话设计模式读书笔记--19.责任链模式
    大话设计模式读书笔记--18.命令模式
    大话设计模式读书笔记--17.桥接模式
    大话设计模式读书笔记--17.单例模式
  • 原文地址:https://www.cnblogs.com/bridgestone29-08/p/11600034.html
Copyright © 2020-2023  润新知