Getters,Setters,Constructors及其参数

use*_*173 1 java getter setter constructor

到目前为止,我一直非常乐于助人,尽管我在措辞问题方面并不是很出色.我想我几乎知道自己在做什么,但我正试图了解吸气剂,制定者和构造者之间的关系.我有两个类如下 Student,Name我对getter和setter中的参数与构造函数之间的关系感到困惑

如果我从Name构造函数中删除参数,即这

public Name (){
this.firstName = firstName;
this.lastName = lastName;
Run Code Online (Sandbox Code Playgroud)

并编辑了学生构造函数以反映这一点

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; 
Run Code Online (Sandbox Code Playgroud)

}

我不确定它会产生什么影响.我会很高兴有人可以向我解释我应该/不应该有参数的地方以及为什么?理解这个概念正是阻碍我进一步利用编码进行编码的原因.

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

小智 5

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

在你的情况下Name应该有一个constructor需要firstName,lastName因为这些东西是使Name对象完全初始化的原因.此对象应该像您显示的Address对象一样工作.

否则,如果使用setXXX方法,则Name对象不完整,String初始化为两个对象null或其他未定义状态.