Getters,Setters,Object Java

时间:2015-04-26 07:32:12

标签: java getter-setter

我不确定我的代码有什么问题。我的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);

    }
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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