【问题标题】:Output a List that contains a Class输出一个包含类的列表
【发布时间】:2017-06-16 10:36:35
【问题描述】:

我有

list<Employee> EmployeeList = new list<Employee>();

我希望能够输出创建的对象。我有不同类型的员工。在这个例子中,我使用了 Class Manager 类型。在一些用户输入后,我用

结束该选项
Employee newEmployee = new Employee(name, address);
newEmployee = new Manager(name, address, salary, bonus);
EmployeeList.Add(newEmployee);

如果我尝试使用console.writeline(EmployeeList) 我只是得到了命名空间。(类的类型)所以在这种情况下,mynamespace.Manager

我熟悉列表,但不熟悉使用类作为键/参数。

编辑: 代码并不完美,但总体目标是将员工添加到列表中并按名称顺序显示它们。

class Program
{
    static void Main(string[] args)
    {

        List<Employee> EmployeeList = new List<Employee>();


        bool loop = true;

        while (loop)
        {

        Console.Clear();
        Console.WriteLine("Main Menu");
        Console.WriteLine("1. Add Employee");
        Console.WriteLine("2. Remove Employee");
        Console.WriteLine("3. Display Payroll");
        Console.WriteLine("4. Exit");

        Console.Write("Selection: ");



        string input = Console.ReadLine().ToLower();

            switch (input)
            {
                case "1":
                case "add employee":
                    {

                        Console.WriteLine("Add Employee");
                        Console.WriteLine("1. Full Time");
                        Console.WriteLine("2. Part Time");
                        Console.WriteLine("3. Contractor");
                        Console.WriteLine("4. Salaried");
                        Console.WriteLine("5. Manager");
                        Console.WriteLine("6. Previous Menu");
                        Console.Write("Selection: ");
                        string choice = Console.ReadLine().ToLower();

                        if (choice.Contains("1") || choice.Contains("full time"))
                        {
                            Console.Write("\nEmployee Name: ");
                            string name = Console.ReadLine();
                            while (string.IsNullOrWhiteSpace(name))
                            {
                                Console.WriteLine("Must not be blank");
                                name = Console.ReadLine();
                            }

                            Console.Write("Employee Address: ");
                            string address = Console.ReadLine();
                            while (string.IsNullOrWhiteSpace(address))
                            {
                                Console.WriteLine("Must not be blank");
                                address = Console.ReadLine();
                            }

                            Console.Write("Employee Pay per Hour: ");
                            string pph = Console.ReadLine();
                            decimal payPerHour;

                            while (!decimal.TryParse(pph, out payPerHour))
                            {
                                Console.WriteLine("Must be a decimal");
                                pph = Console.ReadLine();
                            }


                            Employee newEmployee = new Employee(name, address);
                            newEmployee = new FullTime(name, address, payPerHour);
                            EmployeeList.Add(newEmployee);

员工类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TrevorMolano_Project_CE07

{

class Employee : IComparable<Employee>
{


    string name;
    string address;


    public int CompareTo(Employee obj)
    {          
        Employee person = obj;

        return string.Compare(name, obj.name);


    }

    public virtual decimal CalculatePay(decimal _hpw, decimal _pph, decimal _nbb)
    {
        decimal hpw = _hpw;
        decimal pph = _pph;
        decimal answer;
        answer = hpw * pph * 52;
        return answer;

    }

    public Employee(string _name, string _address)
    {
        name = _name;
        address = _address;
    }
}
}

【问题讨论】:

  • 不清楚您要做什么。请提供minimal reproducible example。顺便说一句,实例化Employee 的代码的第一行是完全多余的,因为您在下一行将其重新声明为Manager

标签: c# list console.writeline


【解决方案1】:

是的,这是正确的,因为像这样直接调用 Console.Writeline() 会调用默认的 ToString() 方法,它会执行您观察到的操作。你所要做的就是遍历列表并显示其中的每个对象

foreach(var item in EmployeeList)
{
  console.writeline(item.name +"\t"+item.address);
}

此外,您的 EmployeeList 集合属于 Employee 类型,如下所示

list<Employee> EmployeeList = new list<Employee>();

那么下面的代码块是无效的,除非Manager的类型是Employee

Employee newEmployee = new Employee(name, address);
newEmployee = new Manager(name, address, salary, bonus);
EmployeeList.Add(newEmployee);

【讨论】:

  • 啊,好吧,我不明白我的错误。尝试了一些测试,我认为我走在正确的道路上。谢谢。
【解决方案2】:

如果你在你的类中重写 ToString() 方法,你可以使用

    Console.WriteLine(string.Join(Environment.NewLine, EmployeeList));

类:

    public class Employee
    {
        public string Name { get; set; }
        // ...
        public override string ToString()
        {
            return string.Format("Employee: {0}", Name);
        }
    }

    public class Manager : Employee
    {
        public override string ToString()
        {
            return string.Format("Manager: {0}", Name);
        }
    }

如果您不想覆盖 ToString(),您可以使用 Linq 选择所需的数据:

Console.WriteLine(string.Join(Environment.NewLine, EmployeeList.Select(x => x.Name + " " + x.Salary)));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-15
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多