• 【JAVA】继承和多态的子类和父类代码执行顺序研究


    有以下代码:

    User.java

    public class User {
        private String name;
        private int age;
        {
            System.out.println("A");
        }
    
        static {
            System.out.println("B");
        }
    
        User(){
            System.out.println("C");
        }
    
        User(int age,String name){
            this.age = age;
            this.name = name;
            System.out.println("D");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Student.java

    public class Student extends User{
        {
            System.out.println("a");
        }
    
        static {
            System.out.println("b");
        }
    
        Student(){
            System.out.println("c");
        }
    
        Student(int age,String name){
            super(age,name);
            System.out.println("d");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    问题1

    执行 User user = new Student(12,”Tom”); 会输出什么?

    答案:BbADad(父类中的静态代码、子类中的静态代码段、父类中的非静态代码、父类中的有参构造构造方法、子类中的非静态代码、子类中的有参构造方法)

    问题2

    如果Student.java更改为如下(不使用super),会输出什么?

    public class Student extends User{
        private int age;
        private String name;
        {
            System.out.println("a");
        }
    
        static {
            System.out.println("b");
        }
    
        Student(){
            System.out.println("c");
        }
    
        Student(int age,String name){  //自动调用父类的无参数构造器
            this.age = age;
            this.name = name;
            System.out.println("d");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    答案:BbACad(父类中的静态代码、子类中的静态代码段、父类中的非静态代码、父类中的无参构造方法、子类中的非静态代码、子类中的有参构造方法)

    问题1和问题2在C和D上的区别原因是,使用super时,会调用父类的有参构造方法,而不使用super时,调用子类的构造方法则会自动调用父类的无参数构造方法

    问题3

    如果运行以下代码,输出是什么?

    Student student1 = new Student(13,"Tom");
    Student student1 = new Student(12,"张三");
    
    • 1
    • 2

    或者第二个对象使用多态的形式,输出是什么?

    Student student = new Student(13,"Tom");
    User user = new Student(12,"张三");
    
    • 1
    • 2

    答案:BbADadADad(静态代码段只执行一次)

    问题4

    如果Student.java更改为如下(添加了静态成员 count):

    public class Student extends User{
        static int count = 0;
        {
            System.out.println("a");
        }
    
        static {
            System.out.println("b");
        }
    
        Student(){
            System.out.println("c");
        }
    
        Student(int age,String name){
            super(age,name);
            count ++;
            System.out.println("d");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    运行以下代码,count 两次输出的值分别是什么?

    User user = new Student(12,"张三");
    System.out.println(Student.count);
    Student student = new Student(13,"Tom");
    System.out.println(Student.count);
    
    • 1
    • 2
    • 3
    • 4

    答案:1 2(虽然第一个对象没有使用多态的形式,第二个使用了多态的形式,两者的静态成员count还是同一个)

    如上内容部分根据实际运行结果总结,并不是客观的结果,如有错误,多谢指正。

  • 相关阅读:
    java计算机毕业设计高校科研信息管理系统MyBatis+系统+LW文档+源码+调试部署
    如何解决AI场景下的冯诺伊曼陷阱?
    Unity-Linerenderer画线功能
    【Android笔记56】Android之内容观察者Content Observer介绍及其使用
    [机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度
    Codeforces Round 753 (Div. 3)(集训队加训2)
    Java基础之运算符
    零基础学习CSS
    CVE-2021-3156 漏洞复现笔记
    17、JAVA入门——多态、抽象方法和抽象类
  • 原文地址:https://blog.csdn.net/weixin_43529394/article/details/126690816