C#继承-区分派生对象和基对象

本文关键字:对象 派生 继承 -区 | 更新日期: 2023-09-27 18:20:15

在库存程序中,我创建了类ItemItemCollection,以及派生类Computer : Item

在一个表单中,我创建了一个名为TheseItems(包括一些计算机)的ItemCollectionItem的通用列表),我想显示每个项目ThisItem的属性(如位置、数量等)

在这样做的同时,我还想显示计算机的不同属性(如CPU、HDD和RAM)。到目前为止,我有这个:

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    if (ThisItem is Computer)
    {
        //Display distinct properties of Computer instance “ThisItem”
    }
}

到目前为止,没有任何错误,但我不确定如何继续。有可能做我想做的事吗?

编辑:谢谢,牛排!我担心选角不允许我访问预先分配的属性(只允许分配新的属性),但幸运的是我错了!我现在有:

Computer ThisComputer = ThisItem as Computer;
if (Computer != null)
{
     //Display distinct properties of Computer instance “ThisComputer”
}

我得到的迅速帮助给我留下了深刻的印象。谢谢

C#继承-区分派生对象和基对象

您所拥有的将发挥作用。

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    if (ThisItem is Computer)
    {
        Computer computerItem = (Computer)ThisItem;
        //Display distinct properties of Computer instance “ThisItem”
    }
}

或者使用as关键字进行轻微优化:

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    var computerItem = ThisItem as Computer;
    if (computerItem != null)
    {
        //Display distinct properties of Computer instance “ThisItem”
    }
}

此外,我的一个朋友写了一个很好的实用程序类来帮助解决这个问题。我想我应该把它贴出来,因为它被证明是非常有用的。

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    ThisItem.Match()
            .Case<Computer>(comp => /* Do stuff with your computer */)
            .Case<Television>(tv => /* Do stuff with your television */)
            .Default(item => /* Do stuff with your item */);
}

实用程序类看起来像这样。下面给出了要点,它非常可扩展。

public class Matcher<TMatch>
{
    private readonly TMatch _matchObj;
    private bool _isMatched;
    public Matcher(TMatch matchObj)
    {
        this._matchObj = matchObj;
    }
    public Matcher<TMatch> Case<TCase>(Action<TCase> action) where TCase : TMatch
    {
        if(this._matchObj is TCase)
        {
            this.DoCase(() => action((TCase)this._matchObj));
        }
        return this;
    }
    public void Default(Action<TMatch> action)
    {
        this.DoCase(() => action(this._matchObj));
    }
    private void DoCase(Action action)
    {
        if (!this._isMatched)
        {
            action();
            this._isMatched = true;
        }
    }
}
public static class MatcherExtensions
{
    public static Matcher<TMatch> Match<TMatch>(this TMatch matchObj)
    {
        return new Matcher<TMatch>(matchObj);
    }
}

这是我通常使用的方法。它可能比检查类型然后铸造稍微快一点(我没有证据证明这一点)。

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    var computer = ThisItem as Computer;
    if (computer != null)
    {
         //Display distinct properties of Computer instance “ThisItem”
    }
}

告诉,不要问。

不要问ThisItem的属性,这样你就可以编写它们,告诉它为你编写

foreach (Item ThisItem in TheseItems)
{
    ThisItem.DisplayProperties(Console.out);
}

然后,使DisplayProperties()成为虚拟的,并在Computer中重载它以包含它独有的属性。

是的,只要在IF子句中,转换为派生类,属性就可用。

foreach (Item ThisItem in TheseItems)
{
    //Display properties of Item instance “ThisItem” (Excluded for ease of reading)
    if (ThisItem is Computer)
    {
       var ThisComputer = (Computer) ThisItem;
       Display(ThisComputer.CPU);
       //etc
    }
}