Ivor Horton’s Beginning Java 2, JDK 5 Edition phần 3

Lý do chính cho việc sử dụng vòng lặp trong vòng lặp trong khi nếu bạn muốn chắc chắn rằng mã vòng lặp luôn luôn thực hiện ít nhất một nhiên, bạn có thể và vẫn nên đặt các dấu ngoặc. Tôi khuyên rằng bạn luôn luôn sử dụng niềng răng xung quanh cơ thể của một vòng lặp, ngay cả khi nó chỉ là một tuyên bố duy nhất. | Extending Classes and Inheritance The extends keyword that you use here identifies that Dog is a base class for Spaniel so an object of type Spaniel will have members that are inherited from the Dog class in addition to the members of the Spaniel class that appear in its definition. The breed would be Spaniel for all instances of the class Spaniel although in general the name for each spaniel would be different. The Spaniel class might have some additional data members that characterize the specifics of what it means to be a spaniel. You will see in a moment how you can arrange for the base class data members to be set appropriately. A Spaniel object is a specialized instance of a Dog object. This reflects real life. A spaniel is obviously a dog and will have all the properties of a basic dog but it has some unique characteristics of its own that distinguish it from all the dogs that are not spaniels. The inheritance mechanism that adds all the properties of the base class Dog in this instance to those in the derived class is a good model for the real world. The members of the derived class define the properties that differentiate it from the base type so when you derive one class from another you can think of your derived class as a specification for objects that are specializations of the base class object. Another way of thinking about this is that the base class defines a set of objects and a derived class defines a specific subset of those that have particular defining characteristics. Class Inheritance In summary when you derive a new class from a base class the process is additive in terms of what makes up a class definition. The additional members that you define in the new class establish what makes a derived class object different from a base class object. Any members that you define in the new class are in addition to those that are already members of the base class. For your Spaniel class that you derived from Dog the data members to hold the name and .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
55    678    5    03-06-2024
Đã 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.