Thinking in Java 3rd Edition phần 4

và phiên bản tương lai của Java có thể cung cấp các giải pháp hoàn chỉnh hơn cho sự kiên hồiMột nhóm các loại, mà bạn sẽ sử dụng khá thường xuyên trong chương trình của bạn được điều trị đặc biệt. Bạn có thể nghĩ về những "nguyên thủy" các loại. | This can be called a pure is-a relationship because the interface of a class establishes what it is. Inheritance guarantees that any derived class will have the interface of the base class and nothing less. If you follow the above diagram derived classes will also have no more than the base class interface. Feedback This can be thought of as pure substitution because derived class objects can be perfectly substituted for the base class and you never need to know any extra information about the subclasses when you re using them That is the base class can receive any message you can send to the derived class because the two have exactly the same interface. All you need to do is upcast from the derived class and never look back to see what exact type of object you re dealing with. Everything is handled through polymorphism. Feedback When you see it this way it seems like a pure is-a relationship is the only sensible way to do things and any other design indicates muddled thinking and is by definition broken. This too is a trap. As soon as you start thinking this way you ll turn around and discover that extending the interface which unfortunately the keyword extends seems to encourage is the perfect solution to a particular problem. This could be Chapter 7 Polymorphism 327 termed an is-like-a relationship because the derived class is like the base class it has the same fundamental interface but it has other features that require additional methods to implement While this is also a useful and sensible approach depending on the situation it has a drawback. The extended part of the interface in the derived class is not available from the base class so once you upcast you can t call the new methods Talks to Useful object . Message Useful part c zMu If you re not upcasting in this case it won t bother you but often you ll get into a situation in which you need to rediscover the exact type of the object so you can access the extended methods of that type. The following .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã 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.