gpt4 book ai didi

使用构造函数的java项目无法运行

转载 作者:行者123 更新时间:2023-11-30 01:42:27 25 4
gpt4 key购买 nike

我正在尝试了解构造函数是如何工作的,并且我一直在尝试调试这个简单的java程序,但我无法让它运行。 Eclipse simple 拒绝承认它的存在,只是运行一个早期的项目。任何想法都会非常感激 - 我正在努力看看我做错了什么。

package timber;



public class Person {

private String firstName;
private String lastName;
private String address;
private String username;


public Person(String personFirstName, String personLastName, String personAddress, String personUsername)
{
firstName = personFirstName;
lastName = personLastName;
address = personAddress;
username = personUsername;
}


public void displayPersonDetails()
{
System.out.println("Name: " + firstName + " " + lastName);
System.out.println("Address: " + address);
System.out.println("Username: " + username);
}





}

然后我有一个包含 main 方法的第二个类

package timber;

public class PersonExample {



public void main(String[] args) {

Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
dave.displayPersonDetails();

}
}

最佳答案

could you please add static in main method :-

public static void main(String[] args) {

Person dave = new Person("Dave", "Davidson", "12 Main St.", "DDavidson");
dave.displayPersonDetails();

}

关于使用构造函数的java项目无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59481038/

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