Thinking in Java 4th Edition phần 3

, ví dụ phải điểm tuyệt vời và không thông minh dumbed xuống, giải thích . Ngược lại với nhiều cuốn sách khác Java, tôi tìm thấy nó bất thường trưởng thành, nhất quán, trí tuệ trung thực, cũng bằng văn bản và chính xác. IMHO, một cuốn sách lý tưởng | Simpo PDF Merge and Split Unregistered Version - http package import static . class Stringed extends Instrument public void play Note n print n class Brass extends Instrument public void play Note n print n public class Music2 public static void tune Wind i public static void tune Stringed i public static void tune Brass i public static void main String args Wind flute new Wind Stringed violin new Stringed Brass frenchHorn new Brass tune flute No upcasting tune violin tune frenchHorn . . Output MIDDLE_C MIDDLE_C MIDDLE_C This works but there s a major drawback you must write type-specific methods for each new Instrument class you add. This means more programming in the first place but it also means that if you want to add a new method like tune or a new type of Instrument you ve got a lot of work to do. Add the fact that the compiler won t give you any error messages if you forget to overload one of your methods and the whole process of working with types becomes unmanageable. Wouldn t it be much nicer if you could just write a single method that takes the base class as its argument and not any of the specific derived classes That is wouldn t it be nice if you could forget that there are derived classes and write your code to talk only to the base class That s exactly what polymorphism allows you to do. However most programmers who come from a procedural programming background have a bit of trouble with the way polymorphism works. Exercise 1 2 Create a Cycle class with subclasses Unicycle Bicycle and Tricycle. Demonstrate that an instance of each type can be upcast to Cycle via a ride method. Polymorphism 195 Simpo PDF Merge and Split Unregistered Version - http The twist The difficulty with Music java can be seen by running the program. The output is . This .

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.