private字段配合访问器如何保护数据
java 封装通过把字段设为 private,强制外部代码只能经由公共的访问器(getter/setter)进行读写。这样可以在读取时返回副本或只读视图,在写入时加入合法性校验,从而防止非法状态和意外修改。

访问器中做哪些具体检查与转换
在 Setter 中直接拒绝无效值并抛出异常,在 Getter 中返回不可变对象或深拷贝,避免引用泄露。对于集合类型,使用 Collections.unmodifiableList() 等包装类能确保外部无法修改内部状态。

FilePond 是一个 JavaScript 文件上传库,支持通过拖拽、粘贴和 API 方式处理文件、目录、URL 和 Data URI。它包含内置的图像优化功能、异步分块上传功能,并提供了适用于 React、Vue、Angular 和 Svelte 的适配器。
基于输入为负数时的具体行为验证
当传入负数年龄时,Setter 会抛出 IllegalArgumentException,导致对象创建失败或状态不变;而 Getter 始终返回当前合法值,不会因历史错误调用而改变返回值。
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Person {
private String name;
private int age;
private List<string> hobbies;
public Person(String name, int age) {
setName(name);
setAge(age);
this.hobbies = new ArrayList();
}
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age getHobbies() {
return Collections.unmodifiableList(hobbies);
}
public void addHobby(String hobby) {
if (hobby != null && !hobby.isEmpty()) {
hobbies.add(hobby);
}
}
public static void main(String[] args) {
try {
Person p = new Person("Alice", -5);
} catch (IllegalArgumentException e) {
System.out.println("Caught: " + e.getMessage());
}
Person p2 = new Person("Bob", 30);
p2.addHobby("Reading");
try {
p2.getHobbies().add("Swimming");
} catch (UnsupportedOperationException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
</string>
预期输出:
Caught: Age cannot be negative
Caught: Unmodifiable collection
digraph G {
rankdir=LR;
node [shape=box, style=filled, fillcolor="#f0f0f0", fontsize=16];
edge [fontsize=16];
Client [label="External Code"];
PersonClass [label="Person Class"];
PrivateField [label="private int age", shape=diamond, fillcolor="#ffcccc"];
Setter [label="setAge(int)", shape=ellipse, fillcolor="#ccffcc"];
Validator [label="age >= 0?", shape=diamond, fillcolor="#ffffcc"];
Exception [label="throw Exception", shape=ellipse, fillcolor="#ff9999"];
Success [label="update field", shape=ellipse, fillcolor="#ccffcc"];
Client -> PersonClass [label="call setAge(-5)"];
PersonClass -> Setter;
Setter -> Validator;
Validator -> Exception [label="No"];
Validator -> Success [label="Yes"];
Success -> PrivateField;
}
digraph G {
rankdir=TB;
node [shape=box, style=filled, fontsize=16];
edge [fontsize=16];
InternalState [label="Internal List", shape=ellipse, fillcolor="#e6e6fa"];
UnmodifiableWrapper [label="Collections.unmodifiableList()", shape=parallelogram, fillcolor="#d3d3d3"];
ExternalRef [label="Returned Reference", shape=circle, fillcolor="#f0e68c"];
ModifyAttempt [label="external.add()", shape=ellipse, fillcolor="#ffcccc"];
UnsupportedOperationException [label="UnsupportedOperationException", shape=ellipse, fillcolor="#ff9999"];
InternalState -> UnmodifiableWrapper;
UnmodifiableWrapper -> ExternalRef;
ExternalRef -> ModifyAttempt;
ModifyAttempt -> UnsupportedOperationException;
}
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










