Giải phẫu của Một Lớp
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | bao gói |
| instance variables/ˈɪnstəns ˈveərɪəblz/ | biến thực thể |
Private fields still need checked methods
- A Student has a private score, but
public void setScore(int s) { score = s; }still lets a caller supply -5. Private access alone does not enforce a non-negative score. - A guarded setter can reject that update. Check the constructor and every other method that writes the field too; encapsulation makes a validation policy easier to enforce but does not create it automatically.
Việc biến score thành private tự động từ chối các tham số âm được cung cấp cho một setter công khai không được bảo vệ.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Sai: setter là mã code thuộc lớp và có thể gán giá trị âm. Việc xác thực bắt buộc phải được triển khai.
The three parts of a class
- The class designs here contain instance fields for data, constructors for initial setup, and methods for behavior. These are three useful parts to identify; formally, Java does not classify constructors as class members.
- Trace a Student from construction to an update: the constructor initializes its score field, and an instance method later reads or changes that object's score. A static method instead has no implicit current object.

A class diagram: private attributes and public methods
Instance variables hold state
- Instance variables 实例变量 store each object's own state.
private String name; private int score;— declared inside the class, outside any method.- Every object gets its own copy — one student's
scoreis separate from another's. - These persist for the life of the object (unlike local variables).
private and public
- Access modifiers control who can reach a member.
private— usable only inside the class;public— exposed to other code that can access the class.- Instance variables are usually
private; methods that form the interface arepublic. - This hiding of internal data is called encapsulation 封装.
Ba phần nào tổ chức thiết kế lớp Student được dạy ở đây?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Trường thành viên giữ trạng thái, constructor khởi tạo đối tượng mới, và phương thức cung cấp hành vi. Trong thuật ngữ Java, constructor không phải là thành viên chính thức của lớp.
Biến instance thường được khai báo...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Dữ liệu riêng tư + phương thức công khai = encapsulation (bao gói).
Giữ dữ liệu riêng tư và chỉ exposure nó thông qua các phương thức gọi là ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Encapsulation bảo vệ dữ liệu của một đối tượng.
Hai đối tượng của cùng một lớp chia sẻ một bản sao của mỗi biến instance.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Mỗi đối tượng có bản sao riêng của các biến instance.
Đối với lớp Student cấp cao được hiển thị ở đây, một lớp bên ngoài không liên quan có thể đọc trực tiếp một trường private...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Truy cập private ngăn chặn một lớp bên ngoài không liên quan đọc trực tiếp trường. Mã code bên trong lớp Student có thể đọc trường private của đối tượng Student khác.
Why keep data private
- Encapsulation prevents unrelated external code from directly assigning a private field. Code inside Student can still read or write private fields, including those of another explicitly referenced Student.
- Outsiders interact only through public methods, which can validate and control access.
- You could reject a negative score in a method; a public variable has no such guard.
- Private fields with a deliberately chosen public interface support controlled updates. Each method and constructor must still implement the intended rules; returning a mutable internal object can also expose changes indirectly.
Keep instance variables private; expose behavior through public methods. A public field lets any code set it to anything (a negative score, a null name) with no checks. Encapsulation funnels all access through methods that can validate the change. Each object has separate instance-field storage, but reference fields in different objects may point to the same shared object. Distinguish separate fields from separate referenced objects.
Hai đối tượng có các trường tham chiếu riêng biệt, vì vậy các trường này phải trỏ đến các đối tượng khác nhau.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Sai: các trường tham chiếu riêng biệt có thể cùng trỏ đến cùng một đối tượng có thể thay đổi (mutable).
Hai học sinh tham chiếu cùng một ArrayList ban đầu trống. Một người thêm một mục. Kích thước list mà đối tượng tham chiếu còn lại quan sát được là bao nhiêu?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Cả hai tham chiếu đều trỏ đến cùng một đối tượng danh sách, có kích thước hiện tại là 1.
Anatomy of a Student class:
- Instance variables:
private String name; private int score; - Constructor:
public Student(String n) { ... } - Methods:
public void addScore(int s) { ... },public int getScore() { ... }
Separate field storage from shared objects
- If two Student objects both store a reference to the same ArrayList, their list fields are separate but the referenced list is shared. Adding an item through either reference changes that one list.
- Reassigning one student's list field to a new ArrayList changes only that field. Follow reference arrows when predicting state; private access and object identity answer different questions.
A class has instance variables (each object's state), constructors (set up new objects), and methods (behaviors). Instance variables are usually private and accessed through public methods — this encapsulation protects the data. Each object holds separate instance fields; the objects referenced by those fields may still be shared.
Mỗi đối tượng có trạng thái riêng của nó
a.score và b.score là riêng biệt — mỗi đối tượng sở hữu trạng thái của mình.