获取器、设置器、对象 Java

Getters, Setters , Object Java

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

对于 Person class 中实例化如下的对象,我不知道从哪里开始我的主要方法:

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;
}

当你调用构造函数时,你有 4 个参数,而你的代码中没有这样的构造函数(你有带 3 个参数的构造函数)。