300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java 父类构造函数_Java基础系列 - 子类继承父类 调用父类的构造函数

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

时间:2023-10-25 04:29:10

相关推荐

java 父类构造函数_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);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。