2012-08-05 54 views
1

迄今为止,尽管我在描述问题方面并不擅长,但迄今为止非常有帮助。我想我几乎知道我在做什么,但我试图让我的头在吸气剂,制定者和构造者之间的关系。我有两个类如下 StudentName,我感到困惑的干将的参数和setter方法和构造吸气剂,安装工,施工人员及其参数

之间的关系。如果我从名称构造去掉参数,即该

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

和ASLO编辑的学生构造,以反映这一

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

} 
+0

@BharatSinha这是不好的建议。肯定不需要定义默认构造函数,除非使用Hibernate等。 – 2012-08-05 20:22:53

回答

5

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

你的情况Name应该有一个constructor需要firstNamelastName因为那些东西是什么使完全初始化的对象Name。这个对象应该和你所显示的对象一样工作。

否则,如果您使用setXXX方法,该Name对象是不完整与初始化为null或其他一些不确定的状态下,两个String对象。

+0

谢谢。因此,就初始有效实例而言,初始值是否是我想要的并不重要。我可以使用getter和setter将* initial *值更改为* correct *值。 – user1577173 2012-08-05 17:06:21

+0

“初始值”更适合称为*实例变量/字段*。 Getters用于获取实例字段,setter用于突变这些字段的值。简而言之,你*可以*使用setter来改变实例变量来保存“正确的”值。 – Vulcan 2012-08-05 17:11:26