【问题标题】:How to call a common function in a class instance? [duplicate]如何调用类实例中的常用函数? [复制]
【发布时间】:2014-10-03 13:19:36
【问题描述】:

假设我有两个班级:

class Batman
{
    public void Robin(){...}
    public void Jump(){...}
}

class Superman
{
    public void Kryptonie(){...}
    public void Jump(){...}
}

现在,我有这些类的一个实例:

public object Crossover()
{
     var batman = new Batman();
     var superman = new Superman();

     return superman;
}

我不知道 Crossover 将返回哪个类的实例,它可能是蝙蝠侠或超人。

var someVariableName = Crossover(); //I don't know if this contains an instance of Superman or Batman;

//I do know that no matter which class instance is returned, it will always contain a function named Jump which i want to trigger:
someVariableName.Jump();

现在我知道我可以这样做:

if (someVariableName.GetType() == typeof(Superman)) 
{
    ((Superman) someVariableName).Jump()
}

但是,当我知道保存在该变量中的类的实例将始终包含一个 Jump 函数时,有没有一种方法可以触发 Jump 函数而无需使用 if..else.. 手动检查每种类型?

【问题讨论】:

  • 亲爱的 15K+ 代表回答者,您确定这个问题是独一无二的吗?您不应该花几秒钟时间寻找参考问题而不是回答它吗?
  • @CodeCaster 不过这个有超级英雄。
  • @user2711965 那是两年前的事了。人们学习。不过你是对的,应该作为 How do I Convert a string to an enum in C#? 的副本关闭。
  • @CodeCaster:你是对的。但是结束一个问题并没有什么乐趣。答案是……

标签: c#


【解决方案1】:

使用接口:

interface ISuperHero
{
    void Jump();
}

class Batman : ISuperHero
{
    public void Robin(){...}
    public void Jump(){...}
}

class Superman : ISuperHero
{
    public void Kryptonie(){...}
    public void Jump(){...}
}

然后从你的方法返回接口:

public ISuperHero Crossover()

【讨论】:

    【解决方案2】:

    这就是接口变得有用的地方。考虑这个接口:

    public interface ISuperhero
    {
        void Jump();
    }
    

    还有这些实现:

    class Batman : ISuperhero
    {
        public void Robin(){...}
        public void Jump(){...}
    }
    
    class Superman : ISuperhero
    {
        public void Kryptonie(){...}
        public void Jump(){...}
    }
    

    它们是单独的实现,但它们共享一个通用的多态接口。然后您的函数可以返回该接口:

    public ISuperhero Crossover()
    {
         var batman = new Batman();
         var superman = new Superman();
    
         return superman;
    }
    

    由于该接口有Jump()方法,所以可以直接调用:

    var someVariableName = Crossover();
    someVariableName.Jump();
    

    【讨论】:

      【解决方案3】:

      您可以创建一个定义方法的基类(或一个接口,如果它只是关于方法定义)。然后,您可以覆盖派生类中的实现。

      abstract class ActionFigure
      {
          public abstract void Jump(); // just define it has a Jump method, but implement it in the deriving class
      
          public void SomethingGeneral()
          {
              // no need for an override, just implement it here
          }
      }
      
      class Batman : ActionFigure
      {
          public void Robin(){...}
          public override void Jump(){...}
      }
      
      class Superman : ActionFigure
      {
          public void Kryptonie(){...}
          public override void Jump(){...}
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-04
        • 1970-01-01
        • 2023-03-04
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 2018-03-28
        • 1970-01-01
        相关资源
        最近更新 更多