gpt4 book ai didi

java - 提供编写构造函数的帮助

转载 作者:行者123 更新时间:2023-12-01 16:53:52 24 4
gpt4 key购买 nike

我正在参与一个小组项目,其任务是向现有程序添加构造函数。问题是,我不确定我的队友在问什么。下面是聊天截图 -

showing image

我应该在其中创建一个构造函数:

public class UserMenu extends JFrame {

private JPanel contentPane;
private String a = "";
private UserInfo user;
/**
* Launch the application.
*/

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserMenu frame = new UserMenu(testUser.tes(testUser.tUser));
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

在引用“UserInfo”时(单独的java文件):

public class UserInfo {
private String firstname;
private String lastname;
private String username;
private String password;
private String email;
private double Checkings = 0.0;
private double Savings = 0.0;
private boolean hasCheckings = false;
private boolean hasSaving = false;
public Loan loan;

public UserInfo(String firstname, String lastname, String username, String password, String email) {
this.firstname = firstname;
this.lastname = lastname;
this.username = username;
this.password = password;
this.email = email;
}
}

最佳答案

什么是构造函数:

Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different. People often refer constructor as special type of method in Java.

构造函数与类具有相同的名称,在 Java 代码中看起来像这样。

public class MyClass{
//This is the constructor
MyClass(){
}
..
}

您的案例:

在您的情况下,构造函数将如下所示:

public class UserMenu extends JFrame {

private JPanel contentPane;
private String a = "";
private UserInfo user;
/**
* Launch the application.
*/

public UserMenu(JPanel contentPane, String a, UserInfo user){

this.contentPane = contentPane;
this.a = a;
this.user = user;
}

[...] //rest of the code

如果你想了解更多信息可以查看Constructors in Java

关于java - 提供编写构造函数的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61629818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com