Java getter、setter、构造函数及其参数
到目前为止,你帮了我很大的忙,尽管我的问题措辞不太好。我想我几乎知道我在做什么,但我正试图弄清楚能手、二传手和建设者之间的关系。我有两门课,如下所示Java getter、setter、构造函数及其参数,java,constructor,setter,getter,Java,Constructor,Setter,Getter,到目前为止,你帮了我很大的忙,尽管我的问题措辞不太好。我想我几乎知道我在做什么,但我正试图弄清楚能手、二传手和建设者之间的关系。我有两门课,如下所示 Student和Name我对getter和setter中的参数与构造函数之间的关系感到困惑 如果我从名称构造函数中删除参数,即 public Name (){ this.firstName = firstName; this.lastName = lastName; 并且还编辑了学生构造函数来反映这一点 public Student(int id
Student
和Name
我对getter和setter中的参数与构造函数之间的关系感到困惑
如果我从名称构造函数中删除参数,即
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;
//}
}
构造函数
用于初始化对象实例,以确保在创建时提供有效
对象状态所需的所有最小数据量
在您的情况下,Name
应该有一个constructor
,它需要firstName
和lastName
,因为正是这些东西使Name
对象完全初始化。此对象的工作方式应该与您显示的地址
对象相同
否则,如果使用
setXXX
方法,名称
对象不完整,两个字符串
对象初始化为null
或其他一些未定义的状态。构造函数用于初始化对象实例,以确保在创建时间
在您的情况下,Name
应该有一个constructor
,它需要firstName
和lastName
,因为正是这些东西使Name
对象完全初始化。此对象的工作方式应该与您显示的地址
对象相同
否则,如果使用
setXXX
方法,则Name
对象不完整,两个String
对象初始化为null
或其他未定义状态。谢谢。因此,初始值是否符合我的要求,对于启动的有效实例来说并不重要。我可以使用getter和setter将初始值更改为正确的值,“初始值”更适合称为实例变量/字段。getter用于获取实例字段,setter用于修改这些字段的值。简而言之,您可以使用setter来改变实例变量,以保持“正确”的值。谢谢。因此,初始值是否符合我的要求,对于启动的有效实例来说并不重要。我可以使用getter和setter将初始值更改为正确的值,“初始值”更适合称为实例变量/字段。getter用于获取实例字段,setter用于修改这些字段的值。简言之,您可以使用setter来改变实例变量以保持“正确”的值。@BharatSinha这是错误的建议。当然不需要定义默认构造函数,除非使用Hibernate之类的工具。@BharatSinha这是个错误的建议。除了使用Hibernate之类的工具外,完全不需要定义默认构造函数。