?????? Henry's VB.NET Tour (Nine) - Interface inheritance
??????????????????????????????? Han Rui?
Big Li took my shoulders and said: "You really have imagination, but it is indeed, there are many documents called this way of implementing interfaces as an interface. In fact, the interface can be inherited, in VB The inheritance of the interface between the .NET is called an interface inheritance. "
I can't help but laugh: "The interface inherits to be inherited, of course, I need to use inherits, right?"
Big Li nodded: "Since you are all clear, then you come to simulate a drop-down box comboBOX interface."
"ComboBOX?" I can't help but glave it, but I will try it for a while. "Is it necessary to make it in line with the text box for text input and drop-down list to select a combination of a combination of list items such as a combination?"
Big Li followed me: "The interface is still different from the inheritance of the class in VB.NET, which can support multiple inheritance from multiple interfaces, and the classes in VB.NET only support inheritance of a single base class."
Seeing that Dafu didn't have any other opinions, I started writing code:
?
Interface icontrol
??? Sub paint ()
End interface
Interface ITextBox
Inherits icontrol
'Setting the text in text box
???? Sub setText (ByVal Text As String)
End interface
Interface IListbox
Inherits icontrol
'Set the list item in the drop-down list
???? Sub setItems (byval items () AS String)
End interface
Interface ICOBOX
??? inherits itextbox, IListbox
End interface
Class chenry
??? IMPLEMENTS ICOMBOBOX
???? Sub setText (ByVal Text As String) IMPLEments ITEXTBOX.SETTEXT
??????? 'impleware code
???? End Sub
???? Sub setItems (byval items () as string) IMPLEments IListBox.setItems
??????? 'impleware code
???? End Sub
......
?
Writing this, found that there is a wave line below ICOMBOX in the CHENRY class, indicating that the interface is not achieved, but I have already implemented the methods in the two base ports inherited by ICOMBOBOX. Looking at the mouse close to the wavy line, the system prompt "Sub Paint ()" must be implemented for the interface iControl, so I will continue to write:
Sub Paint () Implements Icontrol.paint
??????????? 'impleware code
??? End Sub
END CLASS?
?
I turned back and asked the big plum: "Is there any basic interface of the interface in the implementation class?"
Large Laoto said: "If the situation is like this exercise, it is of course to achieve the method that does not implement in the base interface. Also, pay attention to the class or structure of the interface implicitly implicitly all of the bases. Interface. If an interface appears multiple times in the passable closure of the base connection, its members are only participated in the derived interface. Implement the type of derivation interface only needs to implement multiple defined base interface methods. So you can use it. Sub Paint () Implements ITEXTBOX.PAINT or Sub Paint () Implements IListBox.paint is replaced, but only one of these three definitions. "Look at this code again." Da Li began to modify it. Code:?
Interface icontrol
??? Sub paint ()
End interface
Interface ITextBox
Inherits icontrol
'Setting the text in text box
Sub setText (ByVal Text As String)
Shadows Sub Paint ()
End interface
Interface IListbox
Inherits icontrol
'Set the list item in the drop-down list
???? Sub setItems (byval items () AS String)
End interface
Interface ICOBOX
??? inherits itextbox, IListbox
End interface
Sub test (Byval X As ICOBOX)
??? x.paint ()
End Sub
?
"What is the method of this interface here? Icontrol is ITEXTBOX?" Da Li's smile, it is really gas. However, I should answer it up. I recall it in the hidden concept of class (see "Overload and Hide"), ha, understand, it is of course called it directly to be derived. Method.
"Is the method in ITEXTBOX!"
"Yes, yes!" Da Li simply praised me, then drank saliva, continue to say: "The member name of the base connection is hidden in a path inheriting the hierarchical structure, but it is not in other paths. Will be hidden, such as we can inherit the Sub Paint () in iControl from ilistbox. "
"But in the Sub Test in this example, X is the instance of the interface? However, the interface has not been implemented?" I still have a problem. "
"TEST method can actually accept any object that implements ICOMBOBOX as a small part parameter, even if the implementation of the interface ICOMBOBOX is very different." Da Li replied.
"Is it saying that we can use the class of ICOMBOBOX interfaces, such as a instance of CHENRY, in place, such as an instance of CHENRY?"
Big Li smiled and said: "It's almost the same, you will slowly think about it. There is a problem more interesting: implement the method or attribute name used to implement the interface, as long as the parameter The list is consistent with the return type. For example, Sub Paint () in chenry is renamed Sub xxx () is also possible, as long as it follows Implements Icontrol.paint, it will be planned. Naming must have planned, otherwise, the interface is inherited I will bring a question that is repeated, let's take a look. "
?
Interface IHENRY1
????Property yyy () AS integer
End interface
Interface IHENRY2
??? Sub YYY (Byvali As Integer)
End interface
Interface Ihenryderived ??? inherits Ihenry1
??? inherits IHENRY2
End interface
SUB Test (byval x ask iryderive)
??? x.yyy (1)
??? x.yyy = 10
End Sub
?
"You see, in Sub Test (), whether you use X.YYY (1) in the definition in Ihenry2, or use X.Yyy = 10 in the way in Ihenry1, the integrated compiler will be placed below them. Wave line, indicating an error, what is wrong? "Da Li asked me, while holding the mouse near the wavy line, there was a compiler error:
"YYY" in the inheritance interface "Ihenry1" and "Ihenry2" is not clear
"So, I have always emphasized the naming rules, right?" Da Li looked at me, "In fact, the solution does not need to change the method and attribute name in the base interface."
SUB Test ()
??? DIM X as IhenryderIved
??? ctype (x, idenry1) .yy = 10
??? ctype (x, idenry2) .yy (1)
End Sub
?
"Oh, you can use the forced type to convert it." I learned a trick again, I couldn't help but evil. But I always have a little uncleoses in my heart, say that this interface is really too much. Hurry and ask: "Big Li Ge, this interface and ..."
"Abstract class?" Da Li took it up: "Don't worry, a young man, look at it, the next building has lunch."
?
-------------------------------------------------- -------------
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/