Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Why Use Interfaces
Message
De
03/01/2008 18:08:29
 
 
À
03/01/2008 17:42:29
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Conception classe
Divers
Thread ID:
01278205
Message ID:
01279372
Vues:
17
Tim,

>Basically Interfaces and Inheritance have a different point to their use.

Yes they do, but Hugo is correct that Interfaces can be used to simulate multiple-inheritance. And there's nothing wrong with using them this way.

Your example with a base Automobile class is good. You have sub-classes, automobileChevrolet and automobileFord. But, now throw your airplane class into the mix. Say that all your classes need to have a start and stop method and a property called engine. Now you're talking some way to implement multiple inheritance. An airplaneCessna would need to inherit from the base airplane class. But that airplane class would also need to implement the same methods and properties that the automobile class implements.

So, your Interface, ILocomotion, might be defined like this:
public interface ILocomotion
{
    string Engine {get;set;}
    void Start();
    void Stop();
}
and your two classes:
public class automobile : ILocomotion
{
    // code to implement the interface
    // plus other automobile-specific code
}
public class airplane : ILocomotion
{
    // code to implement the interface
    // plus other airplane-specific code
}
~~Bonnie




>>Well, nobody mentioned this, so you will hear it from the less appropriate person, but Interfaces are also a way to get some pseudo multiple inheritance (which it can be also bad design) for example (and it will probably stink) You might have two classes, Boat and Car, both deriving from Vehicle but now someone brings you a Car that moves in the water (or a boat that moves in the land), problem is you can not derive the new class from both (some languages do allow to do so) so the Interfaces come to the rescue, you could define a class CarBoat from Vehicle that implements IBoat and ICar. Well, that is a crude explanation, someone will give you a better example and explanation.
>
>Hugo,
>I would consider it more like this:
>If you need to add similar functionality to different types of classes like Car and airplane, then use an interface. They both may need a start and stop function.
>
>However, if you need to add similar functionality to the same type of classes then use inheritance. Like automobile as a base class and then it can be inherited by automobileChevrolet and automobileFord. Both of those classes use most of the base functionality of the base class.
>
>Basically Interfaces and Inheritance have a different point to their use.
>
>All the other examples are excellent also, but thought maybe I could narrow it down to a simple decision.
>Tim
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform