Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# abstract class static field inheritance

I feel like I skipped a C# class or two, but here's my dilemma:

I have an abstract class from which I derive multiple child classes.

I know for sure that for each of the child classes I will have a constructor that needs a certain static object as a model and this object will be different for each of the child classes.

My first approach was to make a public static object in the abstract parent class and then, before I start creating any instances of the child classes, I would modify it for each of them, but it turns out that this way I actually make only ONE static object, for the abstract class, and each of it's child classes uses it.

How could I solve the problem?

To be more exact, here is the pseudocode:

The parent abstract class:

 abstract class AbstractClass
{
   static public ModelObject Model;
   ...
}

One of the child classes:

class Child : AbstractClass
{
    ...
    public Child()
    {
        this.someField = Model.someField;
    }
}

EDIT:

The Model needs to be a member of the "ModelObject" class, it should NOT be a singleton or anything else.

EDIT2:

To be even more exact, i chose this implementation for a game of chess: I have an abstract class for the chess pieces and the child classes represent the concrete pieces of the game: pawns, knights, et cetera.

The abstract class inherits from MeshMatObject, a class that represents generic 3d objects with the basic functionality, like rotations, meshes, materials, textures and so on and it defines abstract methods for chess game pieces, like GetPossibleMoves().

The Model object I was talking about above is a member of the MeshMatObject and, in my opinion, should be defined outside the class just once and then used for all the pieces. I mean: for example all the pawns have the same mesh and texture, so I don't see the point of giving a model as a parameter every time you want to make a pawn.

like image 308
cantrem Avatar asked Aug 22 '10 16:08

cantrem


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

You can get around the shared static field by making your Abstract class generic. Each generic class will get it's own copy of the static fields.

abstract class AbstractClass<T>
{
   static public ModelObject Model;
   ...
}

Then each child class will use a different instance of the static field.

class Child : AbstractClass<Child>
{
    ...
    public Child()
    {
        this.someField = Model.someField;
    }
}

It doesn't matter that AbstractClass doesn't reference the generic parameter. You are only using it to give each child class a unique instance of the base class.

like image 130
shf301 Avatar answered Oct 05 '22 19:10

shf301


I tend to use something similar to @shf301's solution. Depending on your needs it may useful to setup the base class as:

abstract class AbstractClass
{
}

abstract class AbstractClass<TModel> : AbstractClass
    where TModel : ModelObject 
{
   static public TModel Model;
   ...
}

This allows me a common base class that I can work with in non-generic functions. This also allows derived types to choose the exact model type and can cut down on casting.

like image 34
Justin R Avatar answered Oct 05 '22 18:10

Justin R