我试图了解我的统一项目的继承,但似乎发现我的设置存在限制。我在写这篇文章时感到很困惑,因为我仍在学习正确理解 C#。
我有一组继承的类,它们根据两种不同的行为进行划分,这样我就有了正确的引用。
然后我需要对它们进行转换,以便我可以访问这些类之一中的方法。所以我的结构如下所示:
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。然后我迷失了如何正确设置类型。
所以我正在做这样的事情:
//need to set temp :
??? temp;
if(path[i] is Behaviour2){
temp = (Behaviour2)path[i];
} else {
temp = (Behaviour)path[i];
}
temp.reference.Enter();
这里的临时类型应该设置为什么?
我想我可能误解了继承,因为我似乎遇到了类型问题。 C# 是否有解决方案 - 我不能是唯一陷入困境的人。但我的大脑在试图追踪这一切时变得混乱。