MASTERING DELPHI 6 phần 2

Thực tế là Delphi cho phép bạn sử dụng tất cả các phông chữ được cài đặt trên hệ thống của bạn có cả ưu điểm và nhược điểm. Các lợi thế chính là rằng nếu bạn có một số phông chữ tốt đẹp được cài đặt, chương trình của bạn có thể sử dụng bất kỳ trong số họ. | 100 Chapter 3 The Object Pascal Language Inheritance and Polymorphism o Listing Declaration of the three classes of the Animals3 example type TAnimal class public constructor Create function GetKind string function Voice string virtual abstract private Kind string end TDog class TAnimal public constructor Create function Voice string override function Eat string virtual end TCat class TAnimal public constructor Create function Voice string override function Eat string virtual end The most interesting portion of Listing is the definition of the class TAnimal which includes a virtual abstract method Voice. It is also important to notice that each derived class overrides this definition and adds a new virtual method Eat. What are the implications of these two different approaches To call the Voice function we can write the same code as in the previous version of the program How can we call the Eat method We cannot apply it to an object of the TAnimal class. The statement generates the compiler error Field identifier expected. To solve this problem you can use run-time type information RTTI to cast the TAnimal object to a TCat or TDog object but without the proper cast the program will raise an exception. You will see an example of this approach in the next section. Adding the method definition to the TAnimal class is a typical solution to the problem and the presence of the abstract keyword favors this choice. Copyright 2001 SYBEX Inc. Alameda CA Type-Safe Down-Casting 101 Note What happens if a method overriding an abstract method calls inherited In past versions of Delphi this resulted in an abstract method call. In Delphi 6 the compiler has been enhanced to notice the presence of the abstract method and simply skip the inherited call. This means you can safely always use inherited in every overridden method unless you specifically want to disable executing some code of the base

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.