• 【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还是同一个)

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

  • 相关阅读:
    __wakeup绕过版本_PHP__wakeup()方法
    DateTime和DateTimeOffset是同胞兄弟吗?
    Fluent Facede Pattern(外观模式)
    Mac下制作Ubuntu-14.04的U盘启动盘,制作ubuntu-14.04.6-desktop-amd64.iso启动盘
    消息队列 记录
    第十三届蓝桥杯JavaA组、C++A组省赛 J 题——推导部分和 (AC)
    网关限流功能性能优化
    网络爬虫的架构
    使用点链云管家创建瑜伽约课小程序
    java计算机毕业设计Web前端开发技术儿童教育网站MyBatis+系统+LW文档+源码+调试部署
  • 原文地址:https://blog.csdn.net/weixin_43529394/article/details/126690816