小编use*_*173的帖子

此代码是否需要getter和setter才能运行?

我已将下面的所有代码(5个类)包含在一个简单的学生数据库中.

我认为这个代码与认为在Name,AddressStudent类中包含所有当前存在的getter和setter的人相关.我认为代码将在没有它们的情况下运行,并且它们不是必需的并且应该被删除?如果有必要,有人可以解释为什么或有人确认他们没有必要吗?

地址类

public class Address {

private String street, area, city, country;

public Address(String street, String area, String city, String country) {
    this.street = street;
    this.area = area;
    this.street = city;
    this.country = country;
}

public void setStreet(String street) {
    this.street = street;
}

public void setArea(String area) {
    this.area = area;
}

public void setCity(String city) {
    this.city = city;
}

public void setCountry(String country) {
    this.country = country;
}

public String …
Run Code Online (Sandbox Code Playgroud)

java getter setter

3
推荐指数
1
解决办法
8442
查看次数

Getters,Setters,Constructors及其参数

到目前为止,我一直非常乐于助人,尽管我在措辞问题方面并不是很出色.我想我几乎知道自己在做什么,但我正试图了解吸气剂,制定者和构造者之间的关系.我有两个类如下 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 …
Run Code Online (Sandbox Code Playgroud)

java getter setter constructor

1
推荐指数
1
解决办法
7524
查看次数

标签 统计

getter ×2

java ×2

setter ×2

constructor ×1