C#抽象类静态字段继承 [英] C# abstract class static field inheritance

查看:29
本文介绍了C#抽象类静态字段继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我跳过了一两节 C# 课,但这是我的困境:

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.

我该如何解决问题?

更准确地说,这是伪代码:

To be more exact, here is the pseudocode:

父抽象类:

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

其中一个子类:

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

模型需要是ModelObject"类的成员,它不应该是单例或其他任何东西.

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

编辑 2:

更准确地说,我为国际象棋游戏选择了这个实现:我有一个国际象棋棋子的抽象类,子类代表游戏的具体棋子:棋子、骑士等等.

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.

抽象类继承自 MeshMatObject,该类表示具有基本功能的通用 3d 对象,如旋转、网格、材质、纹理等,它定义了国际象棋棋子的抽象方法,如 GetPossibleMoves().

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().

我在上面谈论的模型对象是 MeshMatObject 的成员,在我看来,应该在类外定义一次,然后用于所有部分.我的意思是:例如,所有 pawn 都具有相同的网格和纹理,所以我不认为每次要制作 pawn 时都将模型作为参数有什么意义.

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.

推荐答案

我倾向于使用类似于@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.

这篇关于C#抽象类静态字段继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆