zoukankan      html  css  js  c++  java
  • 关于java中构造方法、实例初始化执行顺序

    引用:https://www.cnblogs.com/jyroy/p/11152367.html

    父类Person
    package Person_exdends;

      class Person {
          String name = "未命名";
          int age = -1;
    
    Person(String name, int age) {
        super();
        System.out.println("开始构造方法person(),此时this.name=" + this.name + "  this.age=" + this.age);
        this.name = name;
        this.age = age;
        System.out.println("person()构造完成,此时this.name=" + this.name + "  this.age=" + this.age);
    }
      }
    

    子类Student
    package Person_exdends;

      class Student extends Person {
          String school = "未定义学校";
    
    Student(String name, int age, String school) {
        super(name, age);
        System.out.println(
                "开始构造方法Student(),此时this.name=" + this.name + "  this.age=" + this.age + " this.school=" + this.school);
        this.school = school;
        System.out.println(
                "Student()构造完成,此时this.name=" + this.name + "  this.age=" + this.age + " this.school=" + this.school);
    }
      }
    

    package Person_exdends;
    
      public class Main {
          public static void main(String[] args) {
              new Student("Tom", 20, "清华");
          }
      }
    

  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/spacexlxl/p/13463849.html
Copyright © 2011-2022 走看看