从继承类调用方法

时间:2015-12-25 13:03:59

标签: c# oop inheritance methods

abstract class Person
{
    public abstract void LoadName(string name);
}

class Soldier : Person
{
    string soldierName;
    int ID;

    public override void LoadName(string name)
    {
        soldierName = name;
    }

    public void LoadName(int id)
    {
        ID = id;
    }
}

class PersonManager
{
    public void LoadNames(Person[] person, string[] names, int[] id)
    {
        for(int i = 0; i < p.Length; i++)
        {
            person[i].LoadName(names[i]);

            if(person[i] is Soldier)
            {
                /* What I want to do:
                person.LoadName(id[someValue]);       
                   -> Cannot convert from 'string' to 'int'

                   or:
                (Soldier)person.LoadName(id[someValue]);
                   -> Only assignment, call, increment, decrement and new object expressions can be used as a statement
                */

                // What I have to do:
                Soldier s = (Soldier)person[i];
                s.LoadName(id[someValue]);
            }
        }
    }
}

有更优雅的方法吗? 我原来的课程要大得多,所以复制它并不理想。

(请注意,此示例与我的项目无关,因此它可能不是最好的示例。)

3 个答案:

答案 0 :(得分:0)

这是一种方式

    abstract class Person
    {
        public abstract void LoadName(string name);
    }

    class Soldier : Person
    {
        public string soldierName { get; set; }
        public int ID { get; set; }

        public override void LoadName(string name)
        {
            soldierName = name;
        }

        public void LoadName(int id)
        {
            ID = id;
        }
    }

    class PersonManager
    {
        public void LoadNames(Person[] person, string[] names, int[] id)
        {
            for (int i = 0; i < p.Length; i++)
            {
                person[i].LoadName(names[i]);

                if (person[i] is Soldier)
                {
                    Person newPerson = new Soldier() { soldierName = names[i], ID = id[i] }; 
                }
            }
        }
    }
​

或者这个

    abstract class Person
    {
        public abstract void LoadName(string name, int id);
    }

    class Soldier : Person
    {
        public string soldierName { get; set; }
        public int ID { get; set; }

        public override void LoadName(string name, int id)
        {
            soldierName = name;
            ID = id;
        }

    }

    class PersonManager
    {
        public void LoadNames(Person[] person, string[] names, int[] id)
        {
            for (int i = 0; i < p.Length; i++)
            {

                if (person[i] is Soldier)
                {
                    person[i].LoadName(names[i], id[i]); 
                }
            }
        }
    }
​

答案 1 :(得分:0)

你可以说:

((Soldier)person[i]).LoadName(id[someValue]);

哪个并不坏。这里的问题是人没有LoadName(int)当然,在父类中创建抽象重载会解决这个问题,但我假设你不想这样做。因此,LoadName(int)Soldier类来说是唯一的,这是获得它的唯一方法。

答案 2 :(得分:0)

另一种简化设计并使代码更自然的方法是将重载添加到(*string)[*index] = c; 抽象类本身。这会强制任何继承类为LoadName(int id)提供一个实现,我认为它是健康的,因为id更适合于标识实体。

Person