使用继承显示List <int>中的元素

问题描述

我对C#还是很陌生,所以我想问一问如何显示使用ihneritance在不同类中创建的列表?可能吗如果没有,我该如何做?现在,一旦我开始使用其他班级,我在Pirmas_ivedimas班级中添加的数字就会消失。我的代码

class Pirmas_ivedimas //Class where im adding my list
{
    public int n,skaicius;
    public List<int> skaiciai = new List<int>();
    public void Iveskite()
    {
        Console.WriteLine("Kiek skaiciu noresite ivesti?");
        n = int.Parse(Console.ReadLine());
        Console.WriteLine("Iveskite skaicius:");
        for (int i = 0; i < n; i++)
        {
            skaicius = int.Parse(Console.ReadLine());
            skaiciai.Add(skaicius);
        }
    }
}

class Skaiciu_Sarasas:Pirmas_ivedimas //Class where i want to display my list
{
    public void skaiciuSarasas()
    {
        Console.WriteLine("Ivesti skaiciai:" + skaiciai.Count);
        skaiciai.ForEach(Console.WriteLine);
    }
}

解决方法

没有示例,很难为您提供帮助。如果您要创建Skaiciu_sarasas类的实例,则并不意味着您要创建Pirmas_ivedimas类的实例

获取数据的功能应在子类中实现。您可以实现在父类“ Pirmas_ivedimas”中打印列表的功能。要从子级访问此功能,请使用Base.NameOfYourPrintFunction()