gpt4 book ai didi

java - Getter、Setter、对象 Java

转载 作者:行者123 更新时间:2023-12-01 11:36:56 24 4
gpt4 key购买 nike

我不确定我的代码有什么问题。我的代码 Person类如下所示。

我不知道从哪里开始我的 main 方法,对于像这样实例化的 Person 类中的对象:

newPerson = new Person( 
"Richard Pelletier",
"1313 Park Blvd",
"San Diego, CA 92101",
"(619) 388-3113" );

人:

public class Person 
{
private String name;
private String address;
private String cityStateZip;
private String phone;

public Person(){}

public Person( String name,
String address,
String phone )
{
this.name = name;
this.address = address;
this.phone = phone;
}

public void setName( String name )
{
this.name = name;
}

public void setAddress( String address )
{
this.address = address;
}

public void setPhone( String phone )
{
this.phone = phone;
}

public String getName()
{
return name;
}

public String getAdress()
{
return address;
}

public String getPhone()
{
return phone;
}

public String toString()
{

return ("" + this.name + "" + this.address + "" + this.phone);

}
}

最佳答案

我假设您有编译器错误。您的构造函数仅接受三个 String 参数,而您正尝试传递四个参数。尝试添加以下构造函数(或替换现有的构造函数):

public Person( String name,
String address,
String cityStateZip,
String phone )
{
this.name = name;
this.address = address;
this.cityStateZip = cityStateZip;
this.phone = phone;
}

关于java - Getter、Setter、对象 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874961/

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