Getters,Setters,Constructors及其参数

时间:2012-08-05 16:26:22

标签: java constructor setter getter

到目前为止,你们一直非常乐于助人,尽管我在措辞问题方面并不是很出色。我想我几乎知道自己在做什么,但我正试图了解吸气剂,制定者和构造者之间的关系。我有两个课程如下 StudentName我对getter和setter中的参数与构造函数之间的关系感到困惑

如果我从Name构造函数中删除参数,即此

public Name (){
this.firstName = firstName;
this.lastName = lastName;

并且编辑了学生构造函数以反映这个

public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course,  int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(); //I have removed the parameters here
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage; 

}

我不确定它会产生什么影响。我会很高兴有人可以向我解释我应该/不应该有参数的地方以及为什么?理解这个概念正是阻碍我从目前的编码中进一步发展的。

public class Student {
    private Name name; // This is calling from the Name class, giving it the name 'name'
    private Address address; // This calls from Address, giving it the name 'address'

    private char gender;

    private String course, college;

    private int gradePointAverage, id, age, level;

    public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course,  int level, int gradePointAverage){ //This is the list of variables called from the Student class
        this.id = id;
        this.name = new Name(firstName, lastName);
        //this.name = new Name();
        this.name.setFirstName(firstName);
        this.name.setLastName(lastName);
        this.address = new Address(street, area, city, country);
        this.age = age;
        this.gender = gender;
        this.college = college;
        this.course = course;
        this.level = level;
        this.gradePointAverage = gradePointAverage;
    }

    public int getId(){
        return id;
    }

    public String getName(){
        return name.toString();
    }

    public String getAddress(){
        return address.toString();
    }

    public int getAge(){
        return age;
    }

    public char getGender(){
        return gender;
    }

    public String getCollege(){
        return college;
    }

    public int getLevel() {
        return level;
    }

     public String getCourse() {
        return course;
    }

    public int getGradePointAverage() {
        return gradePointAverage;
    }

    public void printStudent() {
        System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
        System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
        System.out.println("Their gender is " + gender + ".");
        System.out.println("The student studies at " + college + " attending classes in " + course + ".");
        System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
        System.out.println();
    }

}

Name

public class Name{

private String firstName, lastName;

public Name (String firstName, String lastName){
//public Name (){
    this.firstName = firstName;
    this.lastName = lastName;
}

public void setFirstName(String firstName){
    this.firstName = firstName;
}

public void setLastName(String lastName){
    this.lastName = lastName;
}

//public String getFirstName() {
//  return firstName;
//}

//public String getLastName() {
//  return lastName;
//}

///** Returns first name concatenated to last name */
//public String toString() {
//  return firstName + " " + lastName;
//}

}

1 个答案:

答案 0 :(得分:5)

constructors用于初始化Object的实例,以确保在创建时提供valid对象状态所需的所有最小数据量。

在您的情况下,Name应该有一个constructor,需要firstNamelastName,因为这些内容是Name对象完全初始化的原因。此对象应该像您显示的Address对象一样工作。

否则,如果使用setXXX方法,则Name对象不完整,其中两个String对象初始化为null或其他一些未定义状态。