在Java中使用get()和set()
我见过几个用于私有字段的get()和set()方法。我也理解为什么要使用它
但我不明白的是,我的Student
类中有私有字段。那么为什么当我删除get()和set()时,它仍然有效(例如,打印方法)
或者它还在工作,因为它在同一个文件类中?但是如果我尝试调用private字段,而没有从外部类定义get()和set()方法。这行不通吗
这是我的get()和set()代码
public class Student {
private String name;
private String address;
private String degreeName;
private String department;
private String yearCommence;
private long studentID;
private static int nextID = 901000 ;
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getDegreeName() {
return degreeName;
}
public String getDepartment() {
return department;
}
public String getYearCommence() {
return yearCommence;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.name = address;
}
public void setDegreeName(String degreeName) {
this.degreeName = degreeName;
}
public void setDepartment(String department) {
this.department = department;
}
public void setYearCommence(String yearCommence) {
this.department = yearCommence;
}
public Student(String name, String address, String degreeName, String department, String yearCommence, long StudentID) {
this.name = name;
this.address = address;
this.degreeName = degreeName;
this.department = department;
this.yearCommence = yearCommence;
this.studentID = nextID++;
}
public Student(String name, String address, String degreeName, String department, long studentID) {
this(name, address, degreeName, department, null, studentID);
}
public Student(String name, String address, String degreeName, long studentID) {
this(name, address, degreeName, null, null, studentID);
}
public Student(String name, String address, long studentID) {
this(name, address, null, null, null, studentID);
}
public Student(String name, long studentID) {
this(name, null, null, null, null, studentID);
}
public Student(long studentID) {
this(null, null, null, null, null, studentID);
}
@Override
public String toString() {
return "StudentInfo {" + "name=" + name + ", address=" + address + ", degreeName=" + degreeName + ", department=" + department + ", commence = " + yearCommence +", "+ " "+ "studentID = " + studentID+ "}";
}
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Student student1 = new Student("Yusuf", "jl.ANU", "IT", "CECS", null, Student.nextID);
students.add(student1);
System.out.println(student1);
Student student2 = new Student("Ning", "jl.Cikini","IT", null, null, Student.nextID);
students.add(student2);
System.out.println(student2);
Student student3 = new Student("Boris", "jl.Babi", Student.nextID);
students.add(student3);
System.out.println(student3);
Student student4 = new Student(null, null, null, null, null, Student.nextID);
students.add(student4);
System.out.println(student4);
}
}
# 1 楼答案
根据定义,私有属性只能由同一类访问。这意味着类本身中的方法可以访问私有属性。任何其他类中的方法都无法访问此类私有属性,因此需要get/set方法
# 2 楼答案
私有字段可以从同一个类访问。如果希望从外部类访问字段,则应使用公共修饰符声明变量
在学生类中,您超越了每次打印对象时都会执行的toString()方法
mutators和accessors用于设置字段的读/写行为(从外部)
我建议您查看java中的访问修饰符
# 3 楼答案
它之所以有效,主要是因为你从来没有使用过那些“能手/二传手”
创建实例并对其调用
toString
(不是直接调用,但System.out.println
将执行此操作)但是,假设您有另一个类,您在其中构建了一个实例
Student student = new Student...
。你有:Java本身不会将对变量的直接访问转换为对getter/setter的调用,您必须这样做。如果编写第一行代码,就不能指望JVM调用
setName
对于这些值可以访问的范围,我让您阅读其他答案。不用再说了
请注意,您使用的是ID参数
您仍然使用
nextID
而不是StudentID
。个人而言,如果我直接使用静态值,我不会要求那个参数。您只需将其从参数列表中删除即可# 4 楼答案
类中可以有没有get或set方法的私有字段。您可以使用您的类,但无法从该类之外设置或获取该字段的值。如果您有一个构造函数,它将这些私有字段作为参数,那么这些字段将在初始化时设置,但在该实例上之后不能更改
# 5 楼答案
所有函数都位于拥有私有属性的类中,因此可以访问所有这些属性,而不需要getter函数。还应该注意,因为
main
函数是static
,所以它只能访问其他static
成员