An ACTING LESSON for your pet parrot -- working with 'Base'
What if we want a really professional stage animal for theatrical performance? Then it is important to speak LOUD – all uppercase, that is. We implement LOUD by overriding the Say () method and uppercasing the argument string s. This in turn requires access to the base method in the mixin – enter Base.
In contrast to the TThis parameter introduced in the previous section, you can't use a class for the TBase parameter, you MUST use an interface at run-time.
Right: Mixin<Parrot, IParrot> or Mixin<IParrot, IParrot>
Wrong: Mixin<Parrot, Parrot> or Mixin<IParrot, Parrot>
For the following implementation
- we override the
Parrot's (orIParrot's, respectively)Saymethod with one that uppercases thestring sand calls the overridden (base) method with that LOUD string - we make the
Parrot'sSaymethod virtual, so that it can be overridden - we access the base
Say ()via theBaseproperty –Base.Say () - the
Baseproperty is provided by the interfaceIParrotas parameter forTBase
Here is an illustration.
If you run the client code
// Don't forget the popsicle to prop up the 'Remotion' assembly var t = typeof (IMixinTarget); var myPetParrot = ObjectFactory.Create<Parrot> (ParamList.Empty); ((IOnThePhoneMixin) myPetParrot).PretendToTalkOnThePhone ();
the output will look like this:
Output |
Method called |
|---|---|
"RING RING" |
Say () |
"RING RING" |
Say () |
"HALLOHO?" |
Say () |
"OH! HI!" |
This.Say () |
"I'm fine! And yourself?" |
Base.Say () |
"Glad to hear that! I love you! Bye!" |
Base.Say () |
Sample code
The sample code for this exercise is located in subversion.
