Java基础系列 - 子类继承父类,调用父类的构造函数


package com.test7;

public class test7 {
    public static void main(String[] args) {
        Son son = new Son(1000, "张三");
        /**
         * 打印显示
             Father的构造函数1000 张三
             Son的构造函数1000 张三
         */
    }
}

class Father {
    private int userId;
    private String userName;

    public Father(int userId, String userName) {
        System.out.println("Father的构造函数" + userId + " " + userName);
        this.userId = userId;
        this.userName = userName;
    }
}

class Son extends Father {
    /**
     * 子类中调用父类的构造函数
     *
     * @param userId
     * @param userName
     */
    public Son(int userId, String userName) {
        super(userId, userName);
        System.out.println("Son的构造函数" + userId + " " + userName);
    }
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM