迄今爲止,儘管我在描述問題方面並不擅長,但迄今爲止非常有幫助。我想我幾乎知道我在做什麼,但我試圖讓我的頭在吸氣劑,制定者和構造者之間的關係。我有兩個類如下 Student
和Name
,我感到困惑的干將的參數和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;
//}
}
@BharatSinha這是不好的建議。肯定不需要定義默認構造函數,除非使用Hibernate等。 – 2012-08-05 20:22:53