In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the relevant operation method of VB.NET interface? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
When developers apply VB.NET for actual development, they find that there are many differences from other languages, such as inheritance. VB.NET, which only supports single inheritance, introduces the concept of interface in order to solve the problem of multiple inheritance. We can define an interface in this way: a VB.NET implementation interface is a virtual class that contains only virtual members.
(1) the virtual class indicates that the interface cannot be instantiated directly. In other words, an interface is just an abstract concept. For example, we say that cars can run, people can run, and horses can run. We can see instantiated cars, people, and horses. But we can define a "running substance". He can be a car, can also make people, horses, but we can not say, "this thing is a running material, but it is not a car, people or horses."
(2) only virtual members are included, indicating that the interface only shows what functions it has and what kind of information it can provide. But we don't know what these functions and information are and how to provide them. It's like a "running substance". We know it can run, but we don't know exactly how it runs.
The reason why the VB.NET implementation interface can partially replace multiple inheritance is that VB.NET only allows one class to inherit from another and can only be this class; but a class can implement one or more interfaces. Because the interface does not implement members, but only names members, there is no path problem of multi-inheritance.
Now that we assume you already know the name of the interface and some basic knowledge about it, let's see when we need to use the interface.
When we are faced with a problem, that is, we have a function that needs to operate instances of different classes to complete a method with the same purpose, we can implement these methods with the same purpose as interfaces. Now let's look at the problems we face. At present, we have some classes on hand that have no inheritance relationship, but these classes can be displayed as strings.
'Books. What can show is the title of the book. Public Class Book Inherits Media Private m_Name As String Public Function Display () As String Return m_Name End Function End Class' LCD display class, which can display the content on the display screen. Public Class LCD Inherits ComputerService Private m_DisplayComment As String Public Function Display () As String Return m_DisplayComment End Function End Class' user class, which shows the full name (last name + first name). Public Class User Inherits Person Private m_FirstName, m_LastName As String Public Function Display () As String Return m_FirstName & "." & m_LastName End Function End Class
Now we want our program (function) to output these displays to the console via Console. Since they are not inherited by the same class, we now have two options in the VB.NET implementation interface.
(1) make a function for each class, corresponding to the display function of a class.
(2) use a function, replace these classes with Object, and implement them with late binding.
Now let's look at the problems of these two approaches.
(1) the code is complex, and if other classes are added, we have to do a function.
(2) it is not safe. An exception is thrown if the developer passes an instance that does not have a corresponding method.
Now let's take a look at the interface. The interface does not exist according to the inheritance of the class, so we need to define an interface first. It contains a Display method. This means that all instances that conform to this interface must have a method called Display, which has no parameters and returns a string.
Public Interface IDisplayer Function Display () As String End Interface
This Display method is just a virtual function with no content, because we don't know how they should be Display. But we can guarantee that he can be Display. That's enough. Now we use this interface to encapsulate our three classes. Let them implement this interface, and at the same time we must implement all the virtual programs in the interface. This is equivalent to telling the compiler that my class conforms to the function specified by the interface, I can Display, and I'll tell you how to Display.
'Books. What can show is the title of the book.
Public Class Book
Inherits Media
Implements IDisplayer
Private m_Name As String
Public Function Display () As String
Implements IDisplayer.Display
Return m_Name
End Function
End Class
LCD monitor class, which can display the content on the screen of the monitor.
Public Class LCD
Inherits ComputerService
Implements IDisplayer
Private m_DisplayComment As String
Public Function Display () As String
Implements IDisplayer.Display
Return m_DisplayComment
End Function
End Class
'user class, which displays the full name (last name + first name).
Public Class User
Inherits Person
Implements IDisplayer
Private m_FirstName, m_LastName As String
Public Function Display () As String
Implements IDisplayer.Display
Return m_FirstName & "." & m_LastName
End Function
End Class
Now let's start with our display function.
Public Sub Display
(ByVal idr As IDisplayer)
MsgBox (idr.Display)
End Sub
We used the parameter idr in the VB.NET implementation interface, which is of type IDisplayer. We can use interfaces as if we were using classes. What we actually pass in is an instance of a class that implements this interface, but that's not what we care about. All we need to know is that this class can be Display. So we only need to call the interface function Display directly to call the Display function in this interface instance. He must exist because he implements the interface. If it does not exist, the compiler will report an error. This allows us to use the method without knowing the instance type, and it's safe.
If we need to add a new class, such as the Company class, we just need to let it implement the interface, and we can apply this function directly.
Interfaces also allow inheritance, and multiple inheritance is allowed, but interfaces can only inherit from interfaces. For example, our IDisplayer interface inherits two .NET interfaces.
Public Interface IDisplayer Inherits ICloneable, IComparer Function Display () As String End Interface
One is ICloneable, which says our interface supports replication (cloning); the other is IComparer, which says our interface supports comparison.
Now we have a compilation error for these three classes, because we have only implemented the virtual function Display of IDisplayer, and we have not implemented the virtual function of the base interface. So we must also implement virtual members of the base interface. Let's take Book as an example, which needs to be changed a little.
'Books. What can show is the title of the book.
Public Class Book
Inherits Media
Implements IDisplayer
Private m_Name As String
Public Sub New (ByVal Name As String)
M_Name = Name
End Sub
Public Function Display1 () As String
Implements IDisplayer.Display
Return m_Name
End Function
Public Function Compare (ByVal x As Object
ByVal y As Object) As Integer Implements
System.Collections.IComparer.Compare
Dim bx, by As Book
If TypeOf x Is Book AndAlso TypeOf y Is Book Then
Bx = CType (x, Book)
By = CType (y, Book)
Return String.Compare (bx.m_Name, by.m_Name)
End If
End Function
Public Function Clone () As Object
Implements System.ICloneable.Clone
Return New Book (m_Name)
End Function
End Class
The book class actually contains three interfaces: IDisplayer,
ICloneable and IComparer. But when we use it,
The ICloneable and IComparer interfaces will not appear, and its functions will
It is implemented as IDisplayer.
Public Sub Display (ByVal idr As IDisplayer)
MsgBox (idr.Display)
Dim o As Object = idr.Clone
End Sub
When we find some unrelated classes, but have a common operation, its parameters are the same as the return value, and we are going to use them frequently in one (or several) places, we might as well implement these same parts with interfaces. But the prerequisite is that these operations belong to the same operation logically. Do not use VB.NET for the sake of implementing the interface.
This is the answer to the question about the operation method of the VB.NET implementation interface. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.