Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Differences with a destroyer
Message
De
02/12/2008 00:09:40
 
Information générale
Forum:
ASP.NET
Catégorie:
Programmation orienté objet
Versions des environnements
OS:
Windows XP SP2
Network:
Windows XP
Divers
Thread ID:
01364440
Message ID:
01365006
Vues:
10
Great explanation Paul! =0)

~~Bonnie


>>I know they have to be useful. It just hasn't clicked for me yet.
>
>OK, I'll take a stab at this. In a strongly typed language you must define the types that can be passed as parameters to methods. That's OK as long as there is only one specific type your method acts on, or the type you're passing in inherits from the base type of the parameter - it's pretty straightforward.
>
>But what if it doesn't? What if you had a class that wasn't of the correct type to pass into a method and didn't inherit from that type either? Well, you could specify the parameter type as "object", couldn't you? That at least would let you pass it in as a parameter. But now what if your code in the method needs to call a specific method on that parameter? Since .NET is strongly typed you can't do this - it won't compile:
>
>
>public void SomeMethod(object myParam)
>{
>   myParam.SomeOtherMethod();
>}
>
>
>The type "object" doesn't have a "SomeOtherMethod" method. OK, now what? Well, you could jump through a lot of hoops and use reflection to make the method call for you. But it's a lot of work. In VFP code like that is perfectly valid - I can pass any object type into a method like that and access any properties or methods and VFP doesn't care. If the object doesn't have those properties or methods it will just blow up at runtime. I'm a good developer so I won't pass in something that would blow up. .NET sucks - it makes things so difficult.
>
>This would work in VFP:
>
>
>FUNCTION SomeMethod(toParam)
>   toParam.SomeOtherMethod();
>ENDFUNC
>
>
>So now what? I can't do what I'd like to do in .NET. That's where interfaces come in. You define the methods/properties that a class must implement in the interface. Then you use the interface as the parameter type instead. You "inherit" from this interface for every class type that you are going to be passing into this method. You can inherit multiple interfaces on a given type giving your class the cameleon-like ability of appearing as exactly the right type to methods, regardless of what class it really inherits from. So we can rewrite the above code:
>
>
>public interface ISomeInterface
>{
>    void SomeOtherMethod();
>}
>
>public void SomeMethod(ISomeInterface myParam)
>{
>   myParam.SomeOtherMethod();
>}
>
>
>Now assuming the object I pass into SomeMethod implements the ISomeInterface interface, all is well. You get compiler-time checking to make sure things don't blow up, Intellisense works, etc.
Bonnie Berent DeWitt
NET/C# MVP since 2003

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

Click here to load this message in the networking platform