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)Say
method with one that uppercases thestring s
and calls the overridden (base) method with that LOUD string - we make the
Parrot
'sSay
method virtual, so that it can be overridden - we access the base
Say ()
via theBase
property –Base.Say ()
- the
Base
property is provided by the interfaceIParrot
as 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.