TAILIEUCHUNG - Accelerated VB 2005 phần 4

Hội có thể được đặt tên theo hai cách: • mạnh (hoặc đầy đủ) có tên là: Một hội đồng có một cái tên bao gồm bốn phần của tên lắp ráp ngắn, một số phiên bản, nhận dạng văn hóa trong định dạng ISO, và một mã thông báo băm. Nếu lắp ráp được đặt tên với tất cả bốn phần, nó được coi là mạnh mẽ được đặt tên. | 106 CHAPTER 6 INHERITANCE POLYMORPHISM AND ENCAPSULATION Public Class EntryPoint Shared Sub Main Dim b As B New B Dim a As A b End Sub End Class You can see that the previous code introduced a new method on Class B named DoSomething . The astute reader will also notice the addition of the Shadows keyword to the declaration. If you don t add this keyword the compiler will complain with a warning. This is the compiler s way of telling you that you need to be more explicit about the fact that you re hiding a method in the base class. Arguably the compiler does this because hiding members this way is generally considered bad design. Let s see why. The output from the previous code is as follows First of all notice that which DoSomething method gets called depends on the type of reference that it is being called through. This is rather unintuitive since B is-a A and you know that inheritance models an is-a relationship. If that s the case shouldn t the entire public interface for A be available to consumers of the instance of Class B The short answer is yes. If you really want the method to behave differently in subclasses then at the point Class A is defined you would declare the DoSomething method as overridable. That way you could utilize polymorphism to do the right thing. Then the most derived DoSomething would get called no matter which type reference it is called through. In order to declare DoSomething as overridable you need to think about the future at the point you define it. That is you have to think about the possibility that someone could inherit from your class and possibly may want to override this functionality. This is just one reason why inheritance can be more complicated during the design process than it initially seems. As soon as you employ inheritance you have to start thinking about things like this. Even though Class B now hides Class A s implementation

TỪ KHÓA LIÊN QUAN
Đã 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.