Re: double message box again
- From: "Paul E Collins" <find_my_real_address@xxxxxxx>
- Date: Sun, 11 Sep 2005 11:05:01 +0000 (UTC)
"John Salerno" <johnjsal@xxxxxxxxxxxxxxx> wrote:
> Could you give an example of how I might implement the
> is operator for [distinguishing character types]?
Suppose you create an abstract (non-instantiable) base class along
these lines:
abstract class Character
{
public Character()
{
// common setup code for all character types
}
public virtual void CastSpell( ... )
{
// default implementation of casting a spell
}
}
Now all of the *real*, concrete character types can inherit from this,
e.g.
class Barbarian : Character { /* barbarian-specific code in this class
*/ }
class Wizard : Character { /* wizard-specific code in this class */ }
Using the "is" operator, you can then find out the specific type of a
character like this:
Character bob = new Barbarian();
// ...
if (bob is Wizard) { /* some code for if Bob was a wizard */ }
Generally, though, you won't want to test for a subtype explicitly.
(After all, that limits your code to working with the types it already
knows about.) What you can do is use "override" to make modifications
in each subclass. For example, CastSpell up there was declared
"virtual", which means that a subclass can replace it with its own
version, e.g.
class Wizard : Character
{
public override void CastSpell( ... )
{
// special spell-casting for wizards only, instead of the
default
}
}
If you're careful about what goes in the base class and what goes in
subclasses, you can then write your entire game based on the Character
class, and rely on overriding to make individual characters behave as
they should. Then you can add more character classes in the future
without even having to touch the game code, since the capabilities of
the characters are encapsulated inside their individual classes.
Hope that makes sense and isn't too patronising! You might well know a
lot of this stuff already.
P.
.
- Follow-Ups:
- Re: double message box again
- From: John Salerno
- Re: double message box again
- References:
- double message box again
- From: John Salerno
- Re: double message box again
- From: Paul E Collins
- Re: double message box again
- From: John Salerno
- double message box again
- Prev by Date: Restricting a windows textbox to digits only.
- Next by Date: Default Printer
- Previous by thread: Re: double message box again
- Next by thread: Re: double message box again
- Index(es):
Relevant Pages
|