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