上次添加的项目将替换List中的所有现有项目

时间:2017-09-25 18:28:30

标签: c# list inheritance interface

我正在尝试使用从两个接口(IIndividual和IGuardian)继承的类(Student)填充List模板。人口成功相当不错。现在的问题是最后一个元素替换旧元素并覆盖它们。很困惑。

interface IIndividual
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

interface IGuardian
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

class Student: IIndividual, IGuardian
{
    String IIndividual.Name { get; set; }
    String IIndividual.Number { get; set; }
    String IIndividual.Address { get; set; }
    String IGuardian.Name { get; set; }
    String IGuardian.Number { get; set; }
    String IGuardian.Address { get; set; }

    public void output()//testing
    {
        IIndividual i = this as IIndividual;
        Console.WriteLine("Individual Name = {0}", i.Name);
        Console.WriteLine("Individual Number = {0}", i.Number);
        Console.WriteLine("Individual Address= {0}", i.Address);
        IGuardian g = this as IGuardian;
        Console.WriteLine("Guardian Name = {0}", g.Name);
        Console.WriteLine("Guardian Number = {0}", g.Number);
        Console.WriteLine("Guardian Address= {0}", g.Address);
    }
}


static void Main(string[] args)
{
    List<Student> s = new List<Student>();
    Student ss = new Student();
    IIndividual ii = ss as IIndividual;
    IGuardian gg = ss as IGuardian;
    gg.Name = "Kinyo Yemi";
    gg.Number = "08012345678";
    gg.Address = "Bunker Street";

    ii.Name = "Hope Ajayi";
    ii.Number = "08185543124";
    ii.Address = "Metropolitan Council";
    s.Add(ss);
    /////////////////////
    gg.Name = "Awwal Kadi";
    gg.Number = "08072523245";
    gg.Address = "R32 Damboa";

    ii.Name = "Morak Suleiman";
    ii.Number = "08535755543";
    ii.Address = "Sederal Low Cost";
    s.Add(ss);
    foreach (var x in s)
    {
        x.output();
        Console.WriteLine();
    }
    Console.Read();
}

3 个答案:

答案 0 :(得分:3)

因为你只创建一个学生:

Student ss = new Student();

但是你将它添加到列表中两次:

s.Add(ss);
//...
s.Add(ss);

如果您想添加第二个学生,请创建一个新学生:

Student ss2 = new Student();

然后使用值填充对象并将其添加到列表中:

s.Add(ss2);

(或者甚至只是通过再次写ss = new Student();来重新分配相同的变量。)

无论您使用多少类型更改或变量,如果您只创建一个对象实例,那么所有变量都指向同一个实例。

答案 1 :(得分:1)

你没有新学生 - 你要两次加入同一个学生。

第二项任务将覆盖第一项任务。

插入一个新的&#39;新的&#39;在第二个s.add(ss)之后(标记为&#39; /// 此处 ///&#39;)

然后重新分配您的界面...

static void Main(string[] args)
    {
        List<Student> s = new List<Student>();
        Student ss = new Student();
        IIndividual ii = ss as IIndividual;
        IGuardian gg = ss as IGuardian;
        gg.Name = "Kinyo Yemi";
        gg.Number = "08012345678";
        gg.Address = "Bunker Street";

        ii.Name = "Hope Ajayi";
        ii.Number = "08185543124";
        ii.Address = "Metropolitan Council";
        s.Add(ss);
        ////////**here**/////////////
        ss = new Student();
        ii = ss as IIndividual;
        gg = ss as IGuardian;

        /////////////////////
        gg.Name = "Awwal Kadi";
        gg.Number = "08072523245";
        gg.Address = "R32 Damboa";

        ii.Name = "Morak Suleiman";
        ii.Number = "08535755543";
        ii.Address = "Sederal Low Cost";
        s.Add(ss);
        foreach (var x in s)
        {
            x.output();
            Console.WriteLine();
        }
            Console.Read();
    }

答案 2 :(得分:1)

正如其他人所指出的,你只创建一个学生对象,所以当然你不会在列表中看到两个。但是这里有更大的问题。

你在做什么实际上有点奇怪。 IIndividualIGuardian实际上描述了完全相同的内容

interface IIndividual
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

interface IGuardian
{
    String Name { get; set; }
    String Number { get; set; }
    String Address { get; set; }
}

奇怪的是,你的Student类明确地实现了两个:

class Student: IIndividual, IGuardian
{
    String IIndividual.Name { get; set; }
    String IIndividual.Number { get; set; }
    String IIndividual.Address { get; set; }
    String IGuardian.Name { get; set; }
    String IGuardian.Number { get; set; }
    String IGuardian.Address { get; set; }
}

这样做真的有意义吗?

如果您要将接口和类更改为:

,该怎么办?
public interface IIndividual
{
    string Name { get; set; }
    string Number { get; set; }
    string Address { get; set; }
}

public interface IGuardian : IIndividual
{
    // Only things specific to Guardians should be here
    // If there is nothing specific to Guardians, 
    // dont have a IGuardian interface at all

    string WorkPhone { get; set; }
}

public class Student : IIndividual
{
    //Guardian should be a property of a Student
    public IGuardian Guardian { get; set; }

    public string Name { get; set; }
    public string Number { get; set; }
    public string Address { get; set; }
}

public class Guardian: IGuardian
{       
    public string Name { get; set; }
    public string Number { get; set; }
    public string Address { get; set; }
    public string WorkPhone { get; set; }
}

这样做的好处很简单,学生和监护人两个个人对吗?学生不是个人和监护人,学生只是个人。你应该创建一个实现IGuardian的Guardian类,然后让Student拥有IGuardian属性。

您应该考虑的另一件事是output()方法。如果您只想查看对象的字符串表示,可以覆盖对象的ToString()方法。

我制作了一个小提琴here来清理你的代码并稍微展示我的答案。