在Java中使用get()和set()

时间:2017-06-08 09:35:36

标签: java

我见过几个用于私有字段的get()和set()方法。我也理解为什么要使用它。

但我不明白的是,我的Student课程中包含私人字段。那么为什么当我删除get()和set()时,它仍然有效(例如,打印方法)。

或者它仍在工作,因为它在同一个文件类中?但是如果我尝试调用私有字段而不从外部类定义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);
    }
}

5 个答案:

答案 0 :(得分:0)

所有函数都在拥有私有属性的类中,因此可以访问所有函数而无需getter函数。还应注意,由于main函数为static,因此只能访问其他static成员。

答案 1 :(得分:0)

根据定义,私有属性只能由同一个类访问。这意味着类本身中的方法可以访问私有属性。任何其他类中的方法将无法访问此类私有属性,因此需要get / set方法。

答案 2 :(得分:0)

类中可能有私有字段没有get或set方法。您可以使用您的课程,但无法在此课程之外设置或获取此字段的值。如果您有一个将这些私有字段作为参数的构造函数,则这些字段将在初始化时设置,但之后不能在该实例上进行更改。

答案 3 :(得分:0)

它主要是因为你从未使用过那些getter / setter。

您创建实例并在其上调用toString(不是直接,但System.out.println会这样做)

但是,假设您有另一个用于构建实例Student student = new Student...的类。你有:

student.name = name; //This is directly accessing the private field "name"
stuendt.setName(name); //This is using the setter of "name"

Java不会将变量的直接访问转换为getter / setter的调用,您必须这样做。如果你写第一行,你不能指望JVM调用setName

对于可以访问这些值的范围,我让您阅读其他答案。不需要再说一次了。

请注意,您需要使用ID的参数

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

您仍然使用nextID的{​​{1}} insteand。 Personnaly,如果我直接使用静态值,我不会要求该参数。您只需将其从参数列表中删除即可。

答案 4 :(得分:-1)

可以从同一个类访问私有字段。如果要从外部类访问字段,则应使用public修饰符声明变量。

您在学生班级中使用toString()方法,每次打印对象时都会执行该方法。

mutators和accessors用于设置字段(来自外部)的读/写行为。

我建议您查看java中的访问修饰符。