tailieunhanh - The book of visual basic 2005 net insight for classic vb developers 2006 - phần 5

Thiết kế này đảm bảo rằng bạn có thể tái sử dụng những phần phổ biến các chức năng (như mã Connect () phương pháp) mà không cho phép một lập trình để vô tình tạo ra một đối tượng vô nghĩa (như một thể hiện của lớp cơ sở DBRecord). | This design ensures that you can reuse common pieces of functionality like the code for the Connect method without allowing a programmer to inadvertently create a meaningless object like an instance of the base DBRecord class . MustOverride In the previous section you saw how an abstract class can allow you to share code with different derived classes while remaining safely inactive. Abstract classes also play another role as class templates. To understand how this works you need to realize that there are some members that you might want to declare in a MustInherit class even though you can realistically supply the code. For example when you re designing the DBRecord class you might decide that all the classes that derive from DBRecord should have basic SaveData and LoadData methods which gives them the ability to update or retrieve a single record. However you can t actually write the code to perform this task because it depends on the type of record. Here s where the MustOverride keyword fits in. The MustOverride keyword indicates a method whose implementation must be provided by the derived class. In other words a MustOverride method has no code For that reason a MustOverride method can only be placed inside a MustInherit class. Here s an example Public MustInherit Class DBRecord Public Sub Connect ByVal ConnectionString As String Code goes here. End Sub Public MustOverride Sub LoadData Public MustOverride Sub SaveData End Class In this example we assume that the Connect method which is used to open a database connection is standard enough that it can be coded directly into the DBRecord class. However the other declared methods which retrieve and save data from the database have no default implementation that can be given because they depend upon the contents of the database in question and their types. Therefore we leave them to be overriden actually implemented by methods in derived classes. When you define a method in an abstract class with MustOverride you .

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