VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - VB界面工具入门
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:8784, 日期:2001-05-10
An Introduction to Implemented Interfaces in Visual Basic

by Gene Hughson

First, the obligatory jargon:

The "Implements" keyword in Visual Basic allows for interface inheritance, giving programmers access to a form of polymorphism.

If your first reaction to the above is "Huh?", don注释:t worry. The plain language version is this:

The "Implements" keyword in Visual Basic allows an object to specify that it will provide the same set of public properties and methods as another object. This set of properties and methods is referred to as the interface of the other object. By supporting this interface, the new object can be used anywhere that the old object could be used (eg. passed to a function expecting an object of that type). The relationship between the two objects is that objNew is a kind of objOld (which is polymorphism in a nutshell).

Interfaces are a key part of the Component Object Model (COM) and as such, are an integral part of Visual Basic. In fact, if you注释:ve ever made use of the fact that TextBoxes, ComboBoxes, and ListBoxes are all types of Controls (eg. looping through the controls collection and clearing the .Text property of the control if it注释:s a TextBox), then you have some exposure to interface-based programming.

You will often hear interfaces referred to as a "contract". This is because it is just that - a contract between the creator of the object and the user of that object that the object will provide each and every property and method belonging to that interface. The details of the implementation may vary, but the signature (method name, parameters, and returns) may not. This goes to the very heart of binary compatibility, for in COM, the interface is the binary representation of those signatures.

There are many ways to make use of polymorphism and interfaces, but for our purposes, we注释:ll look at the most common - categorizing objects via an interface that contains common behaviors and attributes. Let注释:s assume we注释:re working on a simulation of canines in a kennel. We must implement several different breeds: Beagles, Poodles, Colleys, and Dobermans.

Each breed has it注释:s own set of special behaviors and attributes, such that each merits its own class: Beagles dig, Colleys herd, Dobermans attack intruders, and Poodles display a nasty attitude. However, there are common threads that all share. All have weight, coloring, and size. All bark, eat, and move. By breaking these commonalities out into an interface (IDog) that all can implement, we have a generic way to deal with all breeds at once. We can feed the entire kennel by doing:

    Dim Dog As IDog

    For Each Dog In Kennel
        Dog.Eat
    Next

It is important to realize that we注释:re not inheriting behavior. The code for these common behaviors must be in each class. That implementation code may differ from one class to another. In fact, for our example, the implementation of the .Bark method should differ: our Poodle will give a yip, the Beagle and Colley will give a more "average" bark, and the Doberman will be something else entirely different.

Another important thing to realize is that the members of an implemented interface do not automatically become part of the default interface for a class (the public properties and methods of a class constitute its default interface). In other words, if our Beagle class has only one public method, .Dig, we would have to do the following to get to the .Bark method:

    Dim Dog As IDog

    Set Dog = mBeagle
    Dog.Bark

There are a couple ways to avoid this situation. One way is to "mirror" the IDog interface in the default interface (ie. declare the same public methods from the IDog interface in the default interface, and have both point to a common private routine which contains the code that actually does the work). Another method is to add the following to the default interface:

Public Property Get Dog() As IDog

    Set Dog = Me

End Property

This allows you to access all of the IDog members in the following manner:

    mBeagle.Dog.Bark
    注释:or
    mBeagle.Dog.Eat

Of course, this only begins to scratch the surface of the possibilities that come with interface-based programming. The more you explore these techniques, the more you realize that there are dozens of ways to put them to use for you.