Professional VB 2005 - 2006 phần 3
Số trang: 110
Loại file: pdf
Dung lượng: 5.87 MB
Lượt xem: 14
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
, kể từ khi sự tách biệt của giao diện từ việc thực hiện của nó là cốt lõi của lập trình hướng đối tượng và thiết kế. Kể từ khi phương pháp này được khai báo là công cộng, đó là có sẵn cho bất kỳ mã bên ngoài lớp học, bao gồm các ứng dụng khác có thể sử dụng lắp ráp.
Nội dung trích xuất từ tài liệu:
Professional VB 2005 - 2006 phần 3 Chapter 5 To include a method as part of your interface, you can simply declare a Public routine: Public Sub AMethod() impo PDF End Sub and Split Unregistered Version - http://www.simpopdf.com Merge Notice that there is no code in this routine. Any code would be implementation and is not part of the interface. Only the declaration of the method is important when discussing interfaces. This can seem confusing at first, but it is an important distinction, since the separation of the interface from its imple- mentation is at the very core of object-oriented programming and design. Since this method is declared as Public, it is available to any code outside the class, including other applications that may make use of the assembly. If the method has a property, you can declare it as part of the interface by using the Property keyword: Public Property AProperty() As String End Property You can also declare events as part of the interface by using the Event keyword: Public Event AnEvent() Finally, you can include actual variables, or attributes, as part of the interface: Public AnInteger As Integer This is strongly discouraged, because it directly exposes the internal variables for use by code outside the class. Since the variable is directly accessible from other code, you give up any and all control over the way the value may be changed or by which code may be accessed. Rather than making any variable Public, it is far preferable to make use of a Property method to expose the value. In that way you can implement code to ensure that your internal variable is only set to valid values and that only the appropriate code has access to the value based on your application’s logic. Using the Native Interface In the end, the native (or primary) interface for any class is defined by looking at all the methods, prop- erties, events, and variables that are declared as anything other than Private in scope. This includes any methods, properties, events, or variables that are inherited from a base class. You’re used to interacting with the default interface on most objects, so this should seem pretty straight- forward. Consider a simple class: Public Class TheClass Public Sub DoSomething() End Sub 188 Inheritance and Interfaces Public Sub DoSomethingElse() End Sub End Class impo PDF Merge and Split Unregistered Version - http://www.simpopdf.com This defines a class and, by extension, also defines the native interface that is exposed by any objects you instantiate based on this class. The native interface defines two methods, DoSomething and DoSomethingElse. To make use of these methods, you simply call them: Dim myObject As New TheClass() myObject.DoSomething() myObject.DoSomethingElse() This is the same thing you’ve done in Chapter 4 and so far in this chapter. However, let’s take a look at creating and using secondary interfaces, because they are a bit different. Secondary Interfaces Sometimes, it can be helpful for an object to have more than one interface, thus allowing you to interact with the object in different ways. Inheritance allows you to create subclasses that are a specialized case of the base class. For example, your Employee is a Person. However, there are times when you have a group of objects that are not the same thing, but you want to be able to treat them as though they were the same. You want all these objects to act as the same thing, even though they are all different. For instance, you may have a series of different objects in an application, product, customer, invoice, and so forth. Each of these would have default interfaces appropriate to each individual object — and each of them is a different class — so there’s no natural inheritance relationship implied between these classes. At the same time, you may need to be able to generate a printed document for each type of object. So, you’d like to make them all act as a printable object. Chapter 7 discusses the is-a and act-as relationships in more detail. To accomplish this, you can define a generic interface that would enable generating such a printed docu- ment. You can call it IPrintableObject. By convention, this type of interface is typically prefixed with a capital I to indicate that it is a formal interface. Each of your application objects can choose to implement the IPrintableObject interface. Every object that implements this interface must provide code to provide actual implementation of the interface, which is unlike inheritance, where the code from a base class is automatically reused. 189 Chapter 5 By implementing this common interface, however, you are able to write a routine that accepts any object that implements the IPrintableObject interface and print it — while remaining totally oblivious to the “real” datatype of the object or the methods its native interface might expose. impo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Before you see ...
Nội dung trích xuất từ tài liệu:
Professional VB 2005 - 2006 phần 3 Chapter 5 To include a method as part of your interface, you can simply declare a Public routine: Public Sub AMethod() impo PDF End Sub and Split Unregistered Version - http://www.simpopdf.com Merge Notice that there is no code in this routine. Any code would be implementation and is not part of the interface. Only the declaration of the method is important when discussing interfaces. This can seem confusing at first, but it is an important distinction, since the separation of the interface from its imple- mentation is at the very core of object-oriented programming and design. Since this method is declared as Public, it is available to any code outside the class, including other applications that may make use of the assembly. If the method has a property, you can declare it as part of the interface by using the Property keyword: Public Property AProperty() As String End Property You can also declare events as part of the interface by using the Event keyword: Public Event AnEvent() Finally, you can include actual variables, or attributes, as part of the interface: Public AnInteger As Integer This is strongly discouraged, because it directly exposes the internal variables for use by code outside the class. Since the variable is directly accessible from other code, you give up any and all control over the way the value may be changed or by which code may be accessed. Rather than making any variable Public, it is far preferable to make use of a Property method to expose the value. In that way you can implement code to ensure that your internal variable is only set to valid values and that only the appropriate code has access to the value based on your application’s logic. Using the Native Interface In the end, the native (or primary) interface for any class is defined by looking at all the methods, prop- erties, events, and variables that are declared as anything other than Private in scope. This includes any methods, properties, events, or variables that are inherited from a base class. You’re used to interacting with the default interface on most objects, so this should seem pretty straight- forward. Consider a simple class: Public Class TheClass Public Sub DoSomething() End Sub 188 Inheritance and Interfaces Public Sub DoSomethingElse() End Sub End Class impo PDF Merge and Split Unregistered Version - http://www.simpopdf.com This defines a class and, by extension, also defines the native interface that is exposed by any objects you instantiate based on this class. The native interface defines two methods, DoSomething and DoSomethingElse. To make use of these methods, you simply call them: Dim myObject As New TheClass() myObject.DoSomething() myObject.DoSomethingElse() This is the same thing you’ve done in Chapter 4 and so far in this chapter. However, let’s take a look at creating and using secondary interfaces, because they are a bit different. Secondary Interfaces Sometimes, it can be helpful for an object to have more than one interface, thus allowing you to interact with the object in different ways. Inheritance allows you to create subclasses that are a specialized case of the base class. For example, your Employee is a Person. However, there are times when you have a group of objects that are not the same thing, but you want to be able to treat them as though they were the same. You want all these objects to act as the same thing, even though they are all different. For instance, you may have a series of different objects in an application, product, customer, invoice, and so forth. Each of these would have default interfaces appropriate to each individual object — and each of them is a different class — so there’s no natural inheritance relationship implied between these classes. At the same time, you may need to be able to generate a printed document for each type of object. So, you’d like to make them all act as a printable object. Chapter 7 discusses the is-a and act-as relationships in more detail. To accomplish this, you can define a generic interface that would enable generating such a printed docu- ment. You can call it IPrintableObject. By convention, this type of interface is typically prefixed with a capital I to indicate that it is a formal interface. Each of your application objects can choose to implement the IPrintableObject interface. Every object that implements this interface must provide code to provide actual implementation of the interface, which is unlike inheritance, where the code from a base class is automatically reused. 189 Chapter 5 By implementing this common interface, however, you are able to write a routine that accepts any object that implements the IPrintableObject interface and print it — while remaining totally oblivious to the “real” datatype of the object or the methods its native interface might expose. impo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Before you see ...
Tìm kiếm theo từ khóa liên quan:
lập trình windows ứng dụng windows tìm hiểu windows lập trình ứng dụng lập trình phần mềm lập trình hệ thống lập trình dotNet lập trình Java lập trình PHPGợi ý tài liệu liên quan:
-
Mô tả công việc lập trình viên phần mềm
1 trang 183 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 162 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 155 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 147 0 0 -
Bài giảng Công nghệ phần mềm - Chương 2: Quy trình xây dựng phần mềm
36 trang 136 0 0 -
Đề cương môn học Phân tích thiết kế phần mềm
143 trang 134 0 0 -
Giáo trình Lập trình Android cơ bản: Phần 1
190 trang 133 0 0 -
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 129 0 0 -
Luận văn : Xây dựng chương trình sắp xếp lịch trực bác sĩ
61 trang 128 0 0 -
Đồ án tốt nghiệp: Bảng LED ma trận điều khiển bằng ứng dụng Android
102 trang 116 0 0