如何在Java中从另一个调用一个构造函数?
您可以使用this()调用另一个构造函数。
示例
这是默认构造函数这是参数化构造函数
public class Sample {
int num;
public Sample() {
System.out.println("This is default constructor");
num = 30;
}
public Sample(int value) { this();
System.out.println("This is parameterized constructor");
num = value;
}
public static void main(String args[]){
Sample s = new Sample(30);
}
}输出结果
This is default constructor This is parameterized constructor
热门推荐
10 同事换单位简短祝福语
11 谢谢朋友的祝福语简短
12 立秋节气简短祝福语
13 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文