面向对象——学生

public class Student {
    int id;
    String name;
    String gender;
    int age;
    
    public Student() {
        System.out.println("=1");
    }
    

    public Student(int id, String name) {
         this(id,name,null);
         System.out.println("=2");
    }

    

    public Student(int id, String name, String gender) {
        //从构造方法调用另一个重载的构造方法,目的是减少代码的重复
        this(id,name,gender,0);
        System.out.println("=3");
    }

    

    public Student(int id, String name, String gender, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        System.out.println("=4");
    }


    public String toString() {
        return "(id:" + id 
                + ", name:" + name 
                + ", gender:" + gender 
                + ", age:" + age + ")";
    }
    
}

你可能感兴趣的:(面向对象——学生)