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-02-02 12:02:37 浏览: 62
可以通过以下代码来实现所需的输出结果:
```
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!
```
相关问题
使用swing编写一个登录页面,登录成功运行public class Test { public static void main(String[] args) { FiveChessFrame f=new FiveChessFrame(); } },用户名和密码都是admin,标题是六子棋
好的,下面是使用swing编写的登录页面的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
private JLabel usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, exitButton;
public Login() {
super("登录页面");
this.setLayout(new GridLayout(3, 2));
usernameLabel = new JLabel("用户名:");
this.add(usernameLabel);
usernameField = new JTextField(10);
this.add(usernameField);
passwordLabel = new JLabel("密码:");
this.add(passwordLabel);
passwordField = new JPasswordField(10);
this.add(passwordField);
loginButton = new JButton("登录");
loginButton.addActionListener(this);
this.add(loginButton);
exitButton = new JButton("退出");
exitButton.addActionListener(this);
this.add(exitButton);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功!");
new FiveChessFrame();
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
}
} else if (e.getSource() == exitButton) {
System.exit(0);
}
}
public static void main(String[] args) {
new Login();
}
}
```
当用户输入正确的用户名和密码后,程序会弹出一个提示框,提示用户登录成功,并且创建一个六子棋的窗口。如果用户名或密码错误,则会弹出一个提示框,提示用户错误信息。
public class Test { public static void main(String[] args) { int i = 0: Integer j = new Integer(0) ; System.out.println(i==j); System.out.println(j.equals(i));
这段代码存在语法错误,不能编译通过。其中第二行中冒号应该改为分号,变量类型应该为 `Integer` 而非 `int`。修改后的代码如下:
```
public class Test {
public static void main(String[] args) {
int i = 0;
Integer j = new Integer(0);
System.out.println(i == j);
System.out.println(j.equals(i));
}
}
```
这段代码首先定义了一个 `int` 类型的变量 `i`,并将其赋值为 `0`;然后定义了一个 `Integer` 类型的变量 `j`,并将其赋值为 `0` 的包装类型 `new Integer(0)`。接下来分别输出了 `i == j` 和 `j.equals(i)` 的结果。
由于 `i` 和 `j` 数据类型不同,因此在比较相等性时会进行类型转换。在 `i == j` 中,`j` 会被自动拆箱,得到 `0`,然后和 `i` 进行比较,因此结果为 `true`。在 `j.equals(i)` 中,由于 `i` 是一个 `int` 类型的基本数据类型,因此会自动装箱为 `Integer` 类型,然后进行比较,因此结果也为 `true`。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)