到目前为止,你们一直非常乐于助人,尽管我在措辞问题方面并不是很出色。我想我几乎知道自己在做什么,但我正试图了解吸气剂,制定者和构造者之间的关系。我有两个课程如下
Student
和Name
我对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;
//}
}
答案 0 :(得分:5)
constructors
用于初始化Object的实例,以确保在创建时提供valid
对象状态所需的所有最小数据量。
在您的情况下,Name
应该有一个constructor
,需要firstName
和lastName
,因为这些内容是Name
对象完全初始化的原因。此对象应该像您显示的Address
对象一样工作。
否则,如果使用setXXX
方法,则Name
对象不完整,其中两个String
对象初始化为null
或其他一些未定义状态。