簡體   English   中英

如何遍歷包含從該抽象 class 派生的非抽象類型的抽象類型列表?

[英]How could I iterate through list of abstract type, containing non-abstract types derived from that abstract class?

我需要遍歷我創建的列表,但不能訪問里面的對象。 我嘗試了一些不同的功能,但沒有任何效果,我擔心我使用了錯誤的工具來完成這項工作。

namespace WholesaleApp
{
 
    internal class Program : Wholesale
    {
        static string filePath = "C:\\Wholesale.txt";
       
        static void Main(string[] args)
        {
            List<Merchandise> productList = new List<Merchandise>();

            productList = ReadFromFile(filePath);
            foreach(Merchandise merchandise in productList)
            {
                //this is where I'm trying to access objects inside and display them
            }
        }
    }
}

這是我的抽象 class:

namespace WholesaleApp
{
    internal abstract class Merchandise
    {
        public string merchandiseName { get; set; }
        public int merchandiseAmount { get; set; }

        public Merchandise(string name, int amount)
        {
            merchandiseName = name;
            merchandiseAmount = amount;
        }
    }
}

這是從Merchandise抽象 class 派生的三個類之一:

namespace WholesaleApp
{
    internal class MerchandiseClothing : Merchandise
    {

        public string clothSize { get; set; }
        public string clothType { get; set; }

        public MerchandiseClothing(string _clothType, string _clothSize, string name, int amount) : base(name, amount)
        {
            clothType = _clothType;
            clothSize = _clothSize;
        }
        
        public void ReturnAll()
        {
            Console.Write(merchandiseName+" of type: "+clothSize+" in amount: "+merchandiseAmount+ " and jeep status is: "+clothType);
        }
    }
}

最后,我的 function 將所有內容添加到最終列表中:

namespace WholesaleApp
{
    internal class Wholesale 
    {
        static public List<Merchandise> ReadFromFile(string filePath)
        {
        List<Merchandise> result = new List<Merchandise>();

        string line;
        StreamReader reader = null!;
            try
            {
                reader = new StreamReader(filePath);
                
                line = reader.ReadLine()!;
                
                while (line != null)
                {
                    string[] words = line.Split(';');
                    if (words[0] == "MerchandiseComputer")
                    {
                        result.Add(new MerchandiseComputer(words[1], words[2], Int32.Parse(words[3])));
                    }
                    else if (words[0] == "MerchandiseCarParts")
                    {
                        result.Add(new MerchandiseCarParts(bool.Parse(words[1]), words[3], words[2], Int32.Parse(words[4])));
                    }
                    else if (words[0] == "MerchandiseClothing")
                    {
                        result.Add(new MerchandiseClothing(words[1], words[2], words[3], Int32.Parse(words[4])));
                    }

                    line = reader.ReadLine()!;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                reader.Close();
            }

            return result;
        }
    }
}

應該可以在這里迭代已經迭代對象。 如果您想使用每個特定 class 中的特定字段,您可以在此處檢查類型並做任何您想做的事情。 例如:

foreach (Merchandise merchandise in productList)
{
    if (merchandise is MerchandiseClothing clothing)
    {
        Console.WriteLine(clothing.clothSize); //Can be use any field from Clothing class 
        Console.WriteLine(clothing.merchandiseAmount); //And also from parent
    }
    else if (merchandise is MerchandiseComputer computer)
    {
        //Do what you want
    }
}

但是,最好在Merchandise class 中創建像WriteToConsole這樣的抽象方法,並在每個實現中覆蓋它。 就像MerchandiseClothing class 中的ReturnAll方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM