【问题标题】:Functions only from baseclass仅来自基类的函数
【发布时间】:2013-01-31 08:56:57
【问题描述】:

我正在制作这款游戏​​,但我遇到了结构问题。我创建了一个名为 Structure 的类,其他类如 Traps、Shelter、Fireplace 都继承自该类。游戏中的瓷砖有自己的类(瓷砖),并在该瓷砖上有一个结构列表。我可以成功地在列表中包含的图块上构建结构。当我尝试从陷阱等类中访问函数时,问题就来了。它不起作用。我只能使用基类 Structure 中的函数。

平铺列表:

class Tile
{
     public List<Structure> Structures = new List<Structure>();
}

我如何建造陷阱或其他建筑:

        bool anyFireplace = Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.OfType<Shelter>().Any();
        if (!anyFireplace)
        {
            woodlogsCost = 4;
            if (Bundle.player.Woodlogs - woodlogsCost >= 0)
            {
                Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.Add(new Shelter(Bundle.player.X, Bundle.player.Y));
                Bundle.player.Woodlogs -= woodlogsCost;
            }
        }

当我绘制结构时(这是我的问题所在,请注意 cmets)

foreach (Structure s in Bundle.map.tile[x, y].Structures)
{
   if (s is Fireplace)
{
   //This is the function from base class Strucure
   s.ColorBody(g, 10, x - minx, y - miny, 0, Brushes.Firebrick);

   // The function that I wan´t to use but can´t be used
   //s.ColorBody(g, x - minx, y - miny); 
}
if (s is Shelter)
{
s.ColorBody(g, 10, x - minx, y - miny, 1, Brushes.ForestGreen);
}
if (s is Sleepingplace)
{
   s.ColorBody(g, 10, x - minx, y - miny, 2, Brushes.Brown);
}
if (s is Trap)
{
   s.ColorBody(g, 10, x - minx, y - miny, 3, Brushes.Silver);
}
if (s is Barricade)
{
   s.ColorBody(g, 10, x - minx, y - miny, 4, Brushes.DarkOliveGreen);
}
}

Soo...我想知道如何访问我不想使用的功能?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    为你的基类添加一个虚方法;

    public class Structure
    {
       public virtual void ColorBody(Graphics g, int someParam1, int someParam2)
       {
           // do nothing in the base class
       }
    }
    

    并覆盖 FireBody 中的方法

    public class FireBody : Structure
    {
        public override void ColorBody(Graphics g, int someParam1, int someParam2)
        {
            // do something here for FireBody
        }
    }
    

    如果所有继承自 Structure 的类都需要它,则将其设为抽象;

    public abstract class Structure
    {
       public abstract void ColorBody(Graphics g, int someParam1, int someParam2);
    }
    

    【讨论】:

    • +1,或者更好的是abstract。这将强制派生类实现它。
    • 这很好用,但 Jasons 的回答也是如此,所以你们俩都得到了 +1。我看到了两种方式的优势,所以我想知道哪一种最好用?有什么性能差异吗?
    • 我认为您不必担心性能差异。基本上没有。但是需要对ColorBody() 进行重载可能表明构建应用程序的更好方法。也许多个 Color() 函数在链中相互调用,而不是仅使用一个 ColorBody()?示例:可能是所有结构都相同的基础ColorStructure()。然后ColorSpecificStructure() 存在于每个结构中但实现方式不同。如果您可以构建它,那么您不需要任何强制转换或 switch 语句。您可以拨打structure.Color()
    【解决方案2】:

    对于计算机来说,s只是Structure。如果您想调用只有 Fireplace 类具有但抽象类 Structure 没有的特定方法(例如,Fireplace 类可能有一个 BurnWood() 方法,这对于 Structure 没有意义有),那么你想让计算机知道这个Structure实际上是也是一个Fireplace(例如)。所以你可以通过铸造来做到这一点;例如:

    ((Fireplace)s).ColorBody(g, x - minx, y - miny); 
    

    (s as Fireplace).ColorBody(g, x - minx, y - miny); 
    

    请参阅 this post,了解强制转换和使用 as 运算符之间的区别。

    【讨论】:

      【解决方案3】:

      正如我从您的问题中注意到的那样,您在两个类中有两个重载函数,即对于基类和派生类,您有不同的数字函数参数

      在这种情况下,您必须支持compile type polymophism,如下所示

       public BaseQueryClass
      {
          public string QueryClassFunc(string mystring, int i)
          {
              return mystring + i.ToString();
          }
      }
      
      public QueryClassDerived : BaseQueryClass
      {
          public string QueryClassFunc(string mystring)
          {
              return mystring;
          }
      }
      

      你可以调用你的方法,如下所示

       BaseQueryClass qClass = new BaseQueryClass();
              qClass.QueryClassFunc("mystring", 1);
      
              ((QueryClassDerived)qClass).QueryClassFunc("test");
      

      希望我在这里没有遗漏任何东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 2020-11-10
        • 2016-09-23
        • 1970-01-01
        相关资源
        最近更新 更多