public class Main { public static void main(String[] args) { Test test = new Test(true); } } class Test { public Test(){ System.out.println("Constructor one invoked!"); } public Test(int x){ System.out.println("Constructor two invoked!"); } public Test(boolean b){ System.out.println("Constructor three invoked!"); } } 使得程序输出结果,如下所示。 Constructor one invoked! Constructor two invoked! Constructor three invoked!
时间: 2024-01-11 09:02:17 浏览: 25
C#中static void Main(string[] args) 参数示例详解
可以通过以下代码实现:
```
public class Main {
public static void main(String[] args) {
Test test1 = new Test();
Test test2 = new Test(1);
Test test3 = new Test(true);
}
}
class Test {
public Test() {
System.out.println("Constructor one invoked!");
}
public Test(int x) {
System.out.println("Constructor two invoked!");
}
public Test(boolean b) {
System.out.println("Constructor three invoked!");
}
}
```
输出结果为:
```
Constructor one invoked!
Constructor two invoked!
Constructor three invoked!
```
阅读全文