杰瑞科技汇

Java中set/get方法有何作用?

Of course! In Java, set and get are fundamental concepts related to accessing and modifying the private fields (variables) of a class. This is a core principle of Encapsulation, one of the four pillars of Object-Oriented Programming (OOP).

Java中set/get方法有何作用?-图1
(图片来源网络,侵删)

Let's break it down.

The Core Idea: Encapsulation

Encapsulation is the idea of bundling the data (fields/attributes) and the methods that operate on that data together within a single unit (a class). It also involves restricting direct access to some of an object's components, which is a key way to prevent the accidental or unauthorized modification of data.

How do we achieve this?

  1. Declare fields as private: This means they can only be accessed from within the class itself.
  2. Provide public methods to access and modify these fields: These methods are commonly called getters and setters.

Getters (get)

A getter is a public method that allows you to retrieve (get) the value of a private field.

Java中set/get方法有何作用?-图2
(图片来源网络,侵删)

Naming Convention

The method name typically starts with get, followed by the capitalized name of the field. For a boolean field, it's common to use is instead of get.

Example

Let's say we have a private field name.

private String name; // This field is private and cannot be accessed directly from outside the class.
// Getter for the 'name' field
public String getName() {
    return this.name; // 'this.name' refers to the field of the current object
}

How to use it:

Person person = new Person();
String personName = person.getName(); // We use the getter to safely get the value

Setters (set)

A setter is a public method that allows you to modify (set) the value of a private field.

Java中set/get方法有何作用?-图3
(图片来源网络,侵删)

Naming Convention

The method name typically starts with set, followed by the capitalized name of the field. It takes one parameter, which is the new value for the field.

Example

Continuing with our name field.

private String name;
// Setter for the 'name' field
public void setName(String newName) {
    // You can add validation logic here!
    if (newName != null && !newName.trim().isEmpty()) {
        this.name = newName;
    } else {
        System.out.println("Invalid name provided.");
    }
}

How to use it:

Person person = new Person();
person.setName("Alice"); // We use the setter to safely change the value

Complete Example: The Person Class

Let's put it all together in a practical example.

public class Person {
    // 1. Private fields (data)
    private String name;
    private int age;
    private boolean isStudent;
    // 2. Constructor to initialize the object
    public Person(String name, int age, boolean isStudent) {
        // Using 'this' to distinguish between the field and the parameter
        this.name = name;
        this.age = age;
        this.isStudent = isStudent;
    }
    // --- GETTERS ---
    /**
     * Returns the name of the person.
     * @return The person's name.
     */
    public String getName() {
        return this.name;
    }
    /**
     * Returns the age of the person.
     * @return The person's age.
     */
    public int getAge() {
        return this.age;
    }
    /**
     * Checks if the person is a student.
     * For booleans, 'is' is often used instead of 'get'.
     * @return true if the person is a student, false otherwise.
     */
    public boolean isStudent() {
        return this.isStudent;
    }
    // --- SETTERS ---
    /**
     * Sets the name of the person.
     * @param newName The new name for the person.
     */
    public void setName(String newName) {
        if (newName != null && !newName.trim().isEmpty()) {
            this.name = newName;
        } else {
            System.out.println("Error: Name cannot be null or empty.");
        }
    }
    /**
     * Sets the age of the person.
     * @param newAge The new age for the person.
     */
    public void setAge(int newAge) {
        if (newAge > 0) {
            this.age = newAge;
        } else {
            System.out.println("Error: Age must be a positive number.");
        }
    }
    /**
     * Sets the student status of the person.
     * @param studentStatus The new student status (true or false).
     */
    public void setStudent(boolean studentStatus) {
        this.isStudent = studentStatus;
    }
    // Optional: A method to display object details
    @Override
    public String toString() {
        return "Person [Name=" + name + ", Age=" + age + ", Is Student=" + isStudent + "]";
    }
}

How to Use the Person Class

public class Main {
    public static void main(String[] args) {
        // Create a new Person object
        Person person1 = new Person("Bob", 25, true);
        // --- GETTERS in action ---
        System.out.println("Initial Details:");
        System.out.println("Name: " + person1.getName());
        System.out.println("Age: " + person1.getAge());
        System.out.println("Is a student? " + person1.isStudent());
        System.out.println("------------------------------------");
        // --- SETTERS in action ---
        System.out.println("Updating details...");
        person1.setName("Robert"); // Valid name
        person1.setAge(26);       // Valid age
        person1.setStudent(false); // Valid status
        // Try to set invalid data to see validation in action
        person1.setName("");      // Will print an error message
        person1.setAge(-5);       // Will print an error message
        System.out.println("------------------------------------");
        // Print the updated details
        System.out.println("Updated Details:");
        System.out.println(person1.toString()); // Using the toString() method for clean output
    }
}

Why Use Getters and Setters? (The Benefits)

  1. Control: You have full control over how your fields are accessed and modified. You can add validation logic inside setters to ensure data integrity (e.g., age can't be negative, name can't be null).

  2. Flexibility: You can change the internal implementation of a class without affecting the code that uses it. For example, you could change a private field String username to two fields String firstName and String lastName. As long as the getUsername() and setUsername() methods work the same from the outside, nothing else breaks.

  3. Security: You can prevent read-only or write-only access. For example, you can provide a getter for a password field but no setter, or vice-versa.

  4. Debugging: Since all access to a field goes through a method, you can easily add logging or breakpoints inside getters and setters to track when and how a field is being changed.

  5. Future-Proofing: You can add logic to getters and setters that you might need later, like triggering an event when a value changes, without changing the public API of your class.


Modern Java: Records (Java 16+)

For simple data carrier classes that are primarily used to hold data (Plain Old Java Objects or POJOs), Java introduced records. A record automatically generates the private final fields, a public constructor, equals(), hashCode(), and toString() methods. It also generates public getters for you, but no setters because the fields are final (immutable).

// A record is a concise way to create an immutable data carrier
public record Point(int x, int y) {}
// How to use it
public class Main {
    public static void main(String[] args) {
        Point p1 = new Point(10, 20);
        // The getters are generated automatically
        System.out.println("X coordinate: " + p1.x()); // Note: accessor is just the field name
        System.out.println("Y coordinate: " + p1.y());
        // You CANNOT create a setter. Records are immutable.
        // p1.x(30); // This will cause a compile-time error!
    }
}

Summary Table

Feature Traditional Class with Getters/Setters Java Record
Purpose General-purpose classes, mutable or immutable data. Immutable data carriers (POJOs).
Fields Can be private, public, static, final, etc. Implicitly private final.
Getters Must be written manually. Automatically generated.
Setters Must be written manually. Not generated (fields are immutable).
Immutability Achievable by making fields final and removing setters. Immutable by design.
Use Case Complex business logic, validation, stateful objects. Simple data transfer objects (DTOs), configuration objects.
分享:
扫描分享到社交APP
上一篇
下一篇