0

So i'm new to the idea of inheritance and interfaces and i'm not sure how to achieve what i'm trying to do.

I have the character class which defines a position, a rotational direction, a texture for a character and then I want to inherit this class with a Player and Enemy class so I can use the same movement and drawing classes for the Players and the Enemies while adding extra functions to control the AI.

So I have the character class which is inherited by the static player class but i'm unable to access the position though the Player class, it says an object reference is required.

So how do I use inheritance to share functions between the player and ai while being able to still access variables in the character class though the child classes?

3
  • 5
    Static player class? No, no, no. You're going to make this hard to test and hard to extend. Make your player class a normal class and both your object reference required error and the difficulties in testing/extending your app will go away. Commented Mar 26, 2012 at 18:27
  • 1
    How can the Player class be static if it inherits Character? A static class can't inherit from anything but object. Commented Mar 26, 2012 at 18:33
  • @BACON, It didn't throw a compiler error it just wasn't helpful Commented Mar 26, 2012 at 18:49

5 Answers 5

3

The parent "Character" class needs to have it's non-public members declared as "protected", rather than "private" if you want the child-classes to be able to access the variables. After that you just use the "base" keyword to reference the parent, similar to the way you would use "this" to reference the current instance of the class.

For example:

//note: abstract is not a pre-requisite for inheritance
abstract class Character
{
     protected float x;
     protected float y;
     protected float z;
     //etc etc
}

public class AIBasic : Character
{
    public void move()
    {
         //purely an example
         base.x++;
         base.y++;
         base.z++;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for telling me about the base keyword, i'll keep that in mind.
2

implement your base class as an abstract class, not a static class. A static method will without a reference to an actual object not be able to access properties or fields of an object.

3 Comments

Thanks, that was helpful, and i'm right in thinking I can use the Player object in place of a Character object because it has inherited it?
i'm not sure what you mean by "in place of", but if Player inherits from Character, it means that any Player instance is also a Character instance.
Yeah that's what I meant thank you, For example I could add a Player object to a list of characters.
2

Inheritance isn't the best solution to this problem. Instead consider using IoC (Inversion of Control) and create separate drawing and moving classes. Then compose those into a single character instance.

Ninject is one of my favorite IoC tools and their tutorial is rather complete. In fact it demonstrates a very simple but common gaming character class.

https://github.com/ninject/ninject/wiki/Injection-Patterns

1 Comment

I don't have very much time to complete this project but I've added that page to my favorites to go have a look at it when I have a bit more time to play around with that. Thanks
1

So the first thing you do is define the base class. If you want to never instantiate it, you can create an abstract class and then allow the properties and methods be overriden:

// Marking the properties as virtual allow you to override their behaviour 'by design'. Their normal
// behaviour will continue to work if you choose not to
public abstract class Character
{
     protected virtual Point Position { get;set; }
     protected virtual int Direction {get;set; }
     protected virtual Image Texture { get;set; }
     protected virtual bool Visible { get; set; }

     // Define methods you would like to partially implement or let the other class do:
     public virtual void Rotate(int directionInDegrees)
     {
          this.Direction += directionInDegrees;
     }

     public abstract void Draw();
} 

// Then use inheritance to create the classes based on this
public class Player : Character
{
     public override void Rotate(int directionInDegrees)
     {
          // Implement character specific rotate
     }

     public void Draw()
     {
          // Must implement Draw(), so that we can instantiate the class properly.
          // All methods of an abstract class must be implemented before a class is deemed
          // valid to use instantiate directly.
     }
}

public class Enemy : Character
{
   public override void Rotate()
   {
   }
}

In answer to 'inheriting' from static classes, you cannot do this. static methods belong to the class, not the instance of the object, so they cannot be inherited. They can be overridden (by use of the new keyword to show you are overriding it), but you would have to implement this for every class you create.

If you want the ability to override methods by default, you have to write methods as non-static.

Comments

0

mark the properties in the base class as protected, as opposed to private... then derived classes can access.

1 Comment

This brings up a problem when I go to use the Character class in functions though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.