tailieunhanh - Beginning Microsoft Visual C# 2008 PHẦN 3

mà rõ ràng là một cái gì đó mà sẽ không làm việc cho mỗi chuỗi có thể. Nếu, ví dụ, bạn cố gắng chuyển đổi một chuỗi như thế số vào một giá trị gấp đôi bằng cách sử dụng (), sau đó bạn sẽ thấy hộp thoại hiển thị trong hình 5-4 khi bạn thực thi mã. | Part I The C Language This indicates that the two types are intended for different purposes. Abstract classes are intended for use as the base class for families of objects that share certain central characteristics such as a common purpose and structure. Interfaces are intended for use by classes that might differ on a far more fundamental level but can still do some of the same things. For example consider a family of objects representing trains. The base class Train contains the core definition of a train such as wheel gauge and engine type which could be steam diesel and so on . However this class is abstract because there is no such thing as a generic train. To create an actual train you add characteristics specific to that train. For example you derive classes such as PassengerTrain FreightTrain and 424DoubleBogey as shown in Figure 9-14. Figure 9-14 A family of car objects might be defined in the same way with an abstract base class of Car and derived classes such as Compact SUV and PickUp. Car and Train might even derive from a common base class such as Vehicle. This is shown in Figure 9-15. Vehicle Ĩ Car Train Compact SUV Pickup PassengerTrain FreightTrain 424DoubleBogey Figure 9-15 234 Chapter 9 Defining Classes Some of the classes lower in the hierarchy may share characteristics because of their purpose not just because of what they are derived from. For example PassengerTrain Compact SUV and Pickup are all capable of carrying passengers so they might possess an IPassengerCarrier interface. FreightTrain and PickUp can carry heavy loads so they might both have an IHeavyLoadCarrier interface as well. This is illustrated in Figure 9-16. Figure 9-16 By breaking down an object system in this way before going about assigning specifics you can clearly see which situations should use abstract classes rather than interfaces and vice versa. The result of this example couldn t be achieved using only interfaces or only abstract inheritance. Struct Types Chapter 8 .