从继承的类在C#转换数据类型 [英] Convert data type from inherited classes in C#
问题描述
我想了解继承我的团结项目,但似乎已经找到了限制我的设置。我得到了我的自我迷惑而写它,因为我还在学习正确理解C#。
I am trying to understand inheritance for my unity project but seem to have found a limitation to my setup. I got my self confused whilst writing it as i am still learning to understand C# properly.
我有一组继承的类,并且他们分手基于这样我有正确的参考两个不同的行为。
I have a set of classes that inherit, and they split based on two different behaviors that way i have the correct reference.
然后我需要扮演他们这样我就可以在这些类中的一个访问方法。所以,我的结构是这样的:
I then need to cast them so i can have access to a method in one of these classes. So my structure looks like this:
public class Behaviour : Position {
public Handler reference;
public Behaviour(int tx, int ty, Handler refer) : base (tx,ty){
reference = refer;
}
// overload
public Behaviour(int tx, int ty) : base (tx,ty){}
}
public class Behaviour2 : Position {
public SettingsHandler reference;
public Behaviour2(int tx, int ty, SettingsHandler refer) : base (tx,ty) {
reference = refer;
}
}
public class SettingsHandler : Handler {
public Settings level {get;set;}
}
public class Handler : MonoBehaviour{
virtual public void Enter(List<Node> n,Vector3 p){}
virtual public void Exit(List<Node> n, Node curNode){}
}
现在这个工作正常,直到我不得不访问Handler.Enter或Handle.Exit。然后,我就失去了对如何正确设置的类型。
Now this was working fine until i had to access Handler.Enter or Handle.Exit. Then i got lost on how to set the type properly.
所以我在做这样的事情:
So I was doing something like this:
//need to set temp :
??? temp;
if(path[i] is Behaviour2){
temp = (Behaviour2)path[i];
} else {
temp = (Behaviour)path[i];
}
temp.reference.Enter();
应该是什么类型的温度设定为在这里?
What should temp type be set to here?
我想我可能误解了继承,因为我似乎得到类型的问题。 C#有一个解决方案 - 我不能谁卡住了唯一的一个。但我的脑子越来越糊涂试图跟随这一切。
I am thinking i might have misunderstood inheritance as i seem to get type issues. Does C# have a solution for this - i can't be the only one who has got stuck. But my brain is getting confused trying to follow it all.
推荐答案
您的问题的原因是,基类设计不当摆在首位,通过以下方式梗:
Your problem stems from the fact that the base classes are poorly designed in the first place, in the following ways:
-
层次结构是没有意义的。行为是不是一种特殊的地位。的 preFER组成继承。的
字段不应该是公开的。的使用性质,不应领域的
Fields should never be public. Use properties, not fields.
是检查是运行时类型检查;不要做多态运行时类型检查;使用虚拟方法。
"is" checks are runtime type checks; don't do runtime type checks for polymorphic behaviour; use virtual methods.
让我们重新设计你的层次。
Let's redesign your hierarchy.
abstract class MyBehaviour
{
public Position Position { get; private set; }
public Handler Handler { get; private set; }
protected MyBehaviour(int x, int y, Handler handler) {
this.Position = new Position(x, y);
this.Handler = handler;
}
}
class Behaviour1 : MyBehaviour {
/* Whatever */
}
class Behaviour2 : MyBehaviour {
/* Whatever */
}
好了,现在我们要执行的处理...
All right, and now when we want to execute the handler...
MyBehaviour b = whatever;
b.Handler.Enter();
完成。没有临时变量需要。没有运行时类型检查。没有如果。行为提供服务;您使用该服务。你不应该问的行为的类型,才能使用它提供的服务;如果这样做,那么可能是在设计层面。
Done. No temporary variable needed. No runtime type check. No "if". The behaviour provides a service; you use the service. You should not have to ask the behaviour its type in order to use the service it provides; if you do, something is probably wrong at the design level.
这篇关于从继承的类在C#转换数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!