从 C# 中继承的类转换数据类型 [英] Convert data type from inherited classes in C#

查看:50
本文介绍了从 C# 中继承的类转换数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解我的 Unity 项目的继承,但似乎发现我的设置存在限制.我在编写它时感到困惑,因为我仍在学习正确理解 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:

  • 层次结构毫无意义.行为不是一种特殊的位置.更喜欢组合而不是继承.

字段不应该是公开的.使用属性,而不是字段.

Fields should never be public. Use properties, not fields.

is"检查是运行时类型检查;不要对多态行为进行运行时类型检查;使用虚拟方法.

"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屋!

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