简体   繁体   English

如何遍历包含从该抽象 class 派生的非抽象类型的抽象类型列表?

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

I need to iterate through list I created, but can't access objects inside.我需要遍历我创建的列表,但不能访问里面的对象。 I tried a few different functions but nothing worked and I'm afraid I'm using the wrong tools for the job.我尝试了一些不同的功能,但没有任何效果,我担心我使用了错误的工具来完成这项工作。

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
            }
        }
    }
}

This is my abstract class:这是我的抽象 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;
        }
    }
}

And this is one of the three classes deriving from Merchandise abstract class:这是从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);
        }
    }
}

Finally, my function where I add everything to the final list:最后,我的 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;
        }
    }
}

It should be possible to iterate here iterate through objects already.应该可以在这里迭代已经迭代对象。 If you want to use specific fields from each specific class, you can put here a check on type and do whatever you want.如果您想使用每个特定 class 中的特定字段,您可以在此处检查类型并做任何您想做的事情。 For example:例如:

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
    }
}

However, better to make abstract method like WriteToConsole in Merchandise class and override it in each implementation.但是,最好在Merchandise class 中创建像WriteToConsole这样的抽象方法,并在每个实现中覆盖它。 Like ReturnAll method in your MerchandiseClothing class就像MerchandiseClothing class 中的ReturnAll方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM