通过this(...)调用兄弟构造器的作用

复制代码
package com.Demo1;

public class Test {
    public static void main(String[] args) {
        //掌握在类的构造器中,通过this(...)调用兄弟构造器的作用
        Student s1=new Student("李四",26,"家里蹲大学");

        //如果学生没有学校,那么学校默认就是黑马程序员
        Student s2=new Student("张三",28);
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
        System.out.println(s2.getSchoolName());

    }
}
class Student{
    private String name;
    private int age;
    private  String schoolName;

    public Student() {
    }

    public Student(String name,int age){
        this(name,age,"黑马程序员");
    }
    public Student(String name, int age, String schoolName) {
        this.name = name;
        this.age = age;
        this.schoolName = schoolName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }
}

 

 


复制代码

 

posted @   Karlshell  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示