Java编程中的构造方法重载
与方法重载类似,构造函数加载是使用不同类型的参数创建和使用构造函数。我们可以使用该运算符来引用构造函数。请参见下面的示例。
示例
class A {
public int a;
public A() {
this(-1);
}
public A(int a) {
this.a = a;
}
public String toString() {
return "[ a= " + this.a + "]";
}
}
public class Tester {
public static void main(String args[]) {
A a = new A(10);
System.out.println(a);
A a1 = new A();
System.out.println(a1);
}
}输出结果
[ a= 10] [ a= -1]
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短