TAILIEUCHUNG - Effective Java Programming Language Guide phần 4

Đây là simplerecipe một: 1. Lưu trữ một số giá trị không đổi khác không, nói 17, trong một kết quả int được gọi là biến. 2. Đối với mỗi f lĩnh vực quan trọng trong đối tượng của bạn (mỗi lĩnh vực được đưa vào tài khoản bằng phương pháp tương đương, có nghĩa là), làm như sau: a. Tính toán một hash c mã int cho lĩnh vực này: | Effective Java Programming Language Guide To make a class immutable follow these five rules 1. Don t provide any methods that modify the object known as mutators . 2. Ensure that no methods may be overridden. This prevents careless or malicious subclasses from compromising the immutable behavior of the class. Preventing method overrides is generally done by making the class final but there are alternatives that we ll discuss later. 3. Make all fields final. This clearly expresses your intentions in a manner that is enforced by the system. Also it may be necessary to ensure correct behavior if a reference to a newly created instance is passed from one thread to another without synchronization depending on the results of ongoing efforts to rework the memory model Pugh01a . 4. Make all fields private. This prevents clients from modifying fields directly. While it is technically permissible for immutable classes to have public final fields containing primitive values or references to immutable objects it is not recommended because it precludes changing the internal representation in a later release Item 12 . 5. Ensure exclusive access to any mutable components. If your class has any fields that refer to mutable objects ensure that clients of the class cannot obtain references to these objects. Never initialize such a field to a client-provided object reference nor return the object reference from an accessor. Make defensive copies Item 24 in contructors accessors and readObject methods Item 56 . Many of the example classes in previous items are immutable. One such class is PhoneNumber in Item 8 which has accessors for each attribute but no corresponding mutators. Here is a slightly more complex example public final class Complex private final float re private final float im public Complex float re float im re im Accessors with no corresponding mutators public float realPart return re public float imaginaryPart return im public Complex add Complex c .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.