Henry VB.NET Tour (8) - Interface

zhaozj2021-02-16  60

?????? Henry's VB.NET trip (8) - interface

??????????????????????????? Han Rui

?

"There are two kinds of inheritance methods?" I was really convinced by VB.NET's powerful feature. The desire for knowledge, drives me to the big smile to Da Li, "Oh, don't smir, I told you that you can't help but laugh.

"When I said that 'fragile base", I mentioned the biggest problem of realizing inheritance. The relationship between the base class and the derived class is too close. Remember? The base class will tend to disclose it, this It is not the package that we are willing to see, so many programming designers try to improve this problem, the most famous, COM. "

"COM? I often use it in VB6, it is a component of a component." I don't know much about Da Li, what is the relationship between this and object-oriented inheritance?

"You said, it is the way of packaging through the components, we can limit the implementation inheritance to the internal use of components, and when we use the components, it will not use what is inside, how to implement it. This is Avoid modification of the base class. We use the organization's organizational procedure method as facing component programming, but this is also an object-oriented design method, but it is more mandatory. Component support components internal implementation inheritance Also support interface inheritance. "

"The interface is inherited, I am not too clear. However, I am clear that the interface is open attribute, method and event, common variable definition method. I have contacted the interface programming in VB6, not too convenient, as if I want to Compiling the method that defines the method but does not write the process into a DLL file, VB6 will automatically create it as an interface, but it can only be hidden, not explicitly defined. "I remembered.

Da Li nodded and said: "From the object-oriented point of view, the interface is the appearance of the object, and the actual work mode of the object is to achieve the interface and implementation. It is the motivation we have to package. Users can only pass Interface to operate, but you can't see the specific implementation code. "

Da Li was a meal, and then said: "VB.NET previous Visual Basic version can use the interface, but you can't create them directly. VB.NET is allowed to use the interface statement to define a real interface!"

I really showed it to this statement. "Can we directly define interfaces?"

"Of course," Da Li said, "In VB.NET, like the class, the interface can also define attributes, methods, and events. But as I just said, the class is different, the interface does not provide realization. Now The interface is implemented by a class and is defined from the class as a separate entity. "

Big Li finger knocked on the desktop, strengthened the tone: "We can understand this, the interface is a convention. The class that implements the interface must be strictly in accordance with each aspect of the interface. The interface can define the functionality as a group of close-related members. Can develop interface enhancements without hazard existing codes, thereby minimizing compatibility issues. You can also be added at any time Interfaces and implementations add new features. Although the interface implementation can evolve, the interface itself cannot be changed once it is published. Change the published interface will destroy the existing code. If the interface is considered, it is clearly agreed Each parties have their own obligations. The issuer of the interface will not change the interface, and the implementation of the interface agrees to implement the interface in strict accordance with the design. "

"That is to say, in VB.NET, the interface is implemented by a class, just like an abstract class, just the keyword is interface, isn't Class, right?" I am still very curious. "The implementation of the interface can be a class or a structure. The definition of the interface is used by the interface keyword. When the implementation is the implementation of the imports keyword" big plum, making me start recalling and structures in my heart. Different (see forebase and structure ").

Big Li then comes to me: "Members of the interface include members of their members and members inherited from their base interfaces. Only nested types, methods, attributes, and events can be used as interface members. Method and attributes cannot have entities. Interface Members are implicitly customized as public, and they cannot specify access to modifiers. The interface can add a modifier. "Da Li followed me, just start writing an example on the computer:

Public interface IHENRY

??? Sub Subx (Byval X as integer)

??? Function Fcny (byval y as integer) AS Long

??? Property Proz () AS STRING

End interface

Public Class Chenry, PUBLIC CLASS CHENRY

??? IMPLEMENTS IHENRY

???Private z1 as string

??? Sub Subx (Byval X as integer) IMPLEments IHENRY.SUBX

??????? 'fill in the current code

??? End Sub

??? Function Fcny (byval y as integer) as long imports Ihenry.FCNY

??????? 'fill in the current code

??? End function

??? Property Proz () AS String Implements Ihenry.proz

??????? get

??????????? Return Z1

??????? End Get

??????? set (ByVal value as string)

??????????? z1 = value

??????? End set

??? End Property

END CLASS

?

?

Big Li pointed to the code: "You see, I define an interface IHENRY with Interface (I General, in general, the first letter of the interface is i, this is not a mandatory meaning, but it is a general purpose Naming rules), including the definition of three methods and properties, but there is no implementation; implement code written in the chenry class. You can understand the idea that I just said, IHENRY interface is actually an outline of a contract, CHENRY is an operating version of the contract, as long as you implement all the methods defined by interface iHenry in chenry, whether it is implemented, there is no new method, you can think that chenry is an implementation that supports the IHenry interface. "

"A type of implementation? That is to say, how many implementations can you?" I didn't understand.

"Of course, I just said that the interface is actually an object of an object. There are many very important interfaces in VB.NET, which defines a lot of types of objects, such as what you are familiar with Windows Form control, their base Most of the class is the Component class, and the Component class is an implementation class for the IComponent interface. The IComponent class has three other implementations, that is: Control, HttPApplication and MarshalByValueComponent class, complete different features, but they all have IComponent The methods and properties defined by the interface, and the parameter definitions are consistent with the signature when the interface is defined. Change the angle to see this problem, how we create a component, and let the system identify, relying on the implementation of the interface To become a class, you must implement the IComponent interface, and provide a basic constructor that does not require parameters or only one type of parameters for IconTainer. "" Oh, I understand, through the interface, we can define some kind of object The basic appearance is then free to implement and expand, but do not involve direct modifications to prototypes. Great! "I suddenly been happy.

"Yes, in VB.NET, you can explicitly define the interface so that the interface programming has become great programming. Just said, the same interface is implemented in different classes, not an interface implementation Multi-state? In addition, multiple interfaces can also be implemented in classes and structures. "Da Li wrote the following code to show me:

?

Interface IONE

??? Sub subone ()

End interface

Interface ITWO

???? Sub Subtwo ()

End interface

Class chenry

???? mplements

????????

???? Sub Subone () IMPLEments IONE.SUBONE

??????? 'impleware code

???? End Sub

???? Sub Subtwo () IMPLEMENTS Itwo.subtwo

??????? 'impleware code

???? End Sub

END CLASS

?

"It's interesting," I looked at the code in my interest, "That is to say that chenry has the appearance characteristics defined by IONE and ITWO."

I suddenly remembered the definition of abstract classes, could not help but curiosity: "If you see the interface as a base class, the class used to implement can not be seen as a derived class? This is the interface inheritance Yeah? "

?

-------------------------------------------------- -------------

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/

转载请注明原文地址:https://www.9cbs.com/read-25528.html

New Post(0)