Re: double message box again

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"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.


.



Relevant Pages

  • Re: Inheritance error: class Foo has no attribute "bar"
    ... character but I keep getting errors. ... I want to "redefine" some attributes from the base class so I can use them ... to help determine the rank. ... and the subclass is called "Marine"): ...
    (comp.lang.python)
  • Re: Inheritance error: class Foo has no attribute "bar"
    ... class Character has no attribute 'attrib_dict' ... expressly in the Marine subclass when I wanted to rename a dictionary ... I want to "redefine" some attributes from the base class so I can use them ... Without seeing your class definitions, it is hard to tell what you are ...
    (comp.lang.python)
  • Re: Inheritance error: class Foo has no attribute "bar"
    ... character but I keep getting errors. ... I want to "redefine" some attributes from the base class so I can use them ... to help determine the rank. ... and the subclass is called "Marine"): ...
    (comp.lang.python)
  • Re: Inheritance error: class Foo has no attribute "bar"
    ... character but I keep getting errors. ... I want to "redefine" some attributes from the base class so I can use them ... to help determine the rank. ... and the subclass is called "Marine"): ...
    (comp.lang.python)
  • Re: Public disclosure of discovered vulnerabilities
    ... > character codes really appear in palatable ... Much of the C language spec was constrained ... specs was that in C99 signed character types are ...
    (sci.crypt)