如何定义默认和非默认构造函数以及mutator和accessor方法

时间:2018-09-06 13:58:43

标签: bluej

在此处创建一个基本类(学生),需要帮助确认默认和非默认构造函数是否正确定义以及如何实现mutator和accessor方法。

public class STUDENT
{
    private String name;
    private String address;
    private String phone_no;
    private String email;

    /**
     * Default constructor for objects of class STUDENT
     */
    public STUDENT()
    {
        name = "";
        address = "";
        phone_no = "";
        email = "";
    }

    /**
     * Non-default constructor for objects of class STUDENT
     */
    public STUDENT (String newName, String newAddress, String newPhone_no, String newEmail)
    {
        name = newName;
        address = newAddress;
        phone_no = newPhone_no;
        email = newEmail;
    }

    /**
     * The mutator method (set) for the field of name.
     */
    public void setName(String name)
    {
        name
    }

    /**
     * The accessor method (get) for the field of name.
     */
    public String getName()
    {
        return
    }
}

1 个答案:

答案 0 :(得分:0)

就构造函数而言,是的,它们已经正确定义。只要构造函数的“签名”是分开的,则该类将进行编译。

关于accessor(get)和mutator(set)方法,使用以下方法:

访问者:

public void getName()
{
   return name;
}

Mutator:

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

遵循此步骤将有助于您的代码:)