我为什么会收到错误方法没有重载DisplayAllPeople需要0个参数?

时间:2014-11-13 01:40:37

标签: c# overloading

public class Program
{
    public static void Main(string[] args)
    {
        GroupOfPeople group = new GroupOfPeople();

        while (IOHelper.AskYesNoQuestion("Do you want to add another person to the array? "))
        {
            Console.Write("Name?   :");
            string name = Console.ReadLine();
            Console.Write("Age?    :");
            int age = int.Parse(Console.ReadLine());
            Person newPerson = new Person(name, age);

            group.AddPerson(newPerson);

        }

        group.DisplayAllPeople();

        Console.Write("Press any key to end the program... ");
        Console.ReadKey();
    }
}

下面是代码的另一部分:

public class GroupOfPeople
{
    private Person[] _people;

    public GroupOfPeople()
    {
        _people = new Person[0];
    }

    public void AddPerson(Person newPerson)
    {
        Person[] _More= new Person[_people.Length +1];
        _More[_people.Length] = newPerson;
        _people = _More;

    }

    public void DisplayAllPeople(Person[] _More)
    {
        foreach (Person i in _More)
        {
            Console.WriteLine(i);
        }

    }
}

忘记添加包含显示方法的这个类 我该如何实现该方法?当我在GroupofPeople类中调用它时,它表示在该上下文中不存在

public class Person
{
    private string _name { get; set; }
    private int _age { get; set; }

    public Person(string name, int age)
    {
        _name = name;
        _age = age;
    }


    public void Display()
    {
        Console.Write("Name : " + _name);
        Console.Write("Age  : " + _age);
        Console.WriteLine();
    }
}

}

3 个答案:

答案 0 :(得分:0)

像这样重新定义DisplayAllPeople:

public void DisplayAllPeople() //make this changes
{
    foreach (Person i in _people)
    {
        Console.WriteLine("name:"i.name+" age:"i.age.ToString());
    }

}

尝试:group.DisplayAllPeople();

答案 1 :(得分:0)

您收到该错误,因为您的方法调用了DisplayAllPeople必需参数并且您没有传递该参数。

我认为你必须做这样的事情。

公共类GroupOfPeople {     私人[] _people;

public GroupOfPeople()
{
    _people = new Person[0];
}

public void AddPerson(Person newPerson)
{
    Person[] _More= new Person[_people.Length +1];
    _More[_people.Length] = newPerson;
    _people = _More;

}

public void DisplayAllPeople()
{
    foreach (Person i in _people)
    {
        Console.WriteLine(i);
    }
}

}

因为您已经在AddPeople方法中为数组添加了值。所以只需使用该数组。

注意:我建议您使用收藏清单。列出而不是数组,这样可以避免数组调整大小问题。

答案 2 :(得分:0)

两种方法

<强> 1。解决方案1 ​​

更改

public void DisplayAllPeople(Person[] _More)
        {
            foreach (Person i in _More)
            {
                Console.WriteLine(i);
            }

        }

到这个

public void DisplayAllPeople()
        {
            foreach (Person i in _people)
            {
                Console.WriteLine(i);
            }

        }

<强> 2。解决方案2

GroupOfPeople中添加以下方法

internal Person[] GetPersons()
{
    return _people;
}

并更改

group.DisplayAllPeople();

到这个

group.DisplayAllPeople(group.GetPersons());

即使您摆脱了错误,最后也不能简单地使用Console.WriteLine(i),其中i是person的实例。

您必须将班级name的{​​{1}}和age属性设为person并使用

public