首页 > 编程语言 > 详细

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

时间:2019-09-19 19:30:01      阅读:77      评论:0      收藏:0      [点我收藏+]
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);
    }
}

  

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

原文:https://www.cnblogs.com/smartsmile/p/11551712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!