2012-08-05 31 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