无法初始化类型

时间:2015-04-04 13:34:20

标签: c#

我需要创建一个类的对象列表,但是会抛出错误! 错误1无法使用集合初始值设定项初始化类型'Lab_5_class_console.Part',因为它未实现'System.Collections.IEnumerable' line parts.Add(new Part(){admin});

出错
namespace Lab_5_class_console
{

    public class Part : IEquatable<administration>
    {
        public administration admin { get; set; }
        public void look()
        {
            admin.Show();
        }
    }
    public abstract class cadre
    {
        public cadre() { }//default constr
        public void Show()
        {
            this.Read();
        }
        ~cadre()
        { Console.WriteLine("Destruct"); }
        abstract public string GetName(string name);
        abstract public string GetProffesion(string proffesion);
        abstract public int GetAge(int age);
        abstract public void Read();
    }
    public class worker : cadre
    {
        private string Cname;
        private string Cproffesion;
        private int Cage;
        private int Csalary;
        public worker()//default constr
        {
            Cname = this.GetName(Cname);
            Cproffesion = this.GetProffesion(Cproffesion);
            Cage = this.GetAge(Cage);
            Csalary = 0;
        }
        ~worker()
        { Console.WriteLine("Destruct"); }
        override public void Read()//construct
        {
            Console.WriteLine("Name: " + this.Cname + '\n' + "Proffesion:" + this.Cproffesion + '\n' + "Age: " + this.Cage + '\n' + "Salaray: " + this.Csalary + '\n');
        }
        public void GetSalary()//construct
        {
            Console.WriteLine("Enter salary: ");
            Csalary = Int32.Parse(Console.ReadLine());
        }
        override public string GetName(string name)
        {
            Console.WriteLine("Enter name: ");
            name = Console.ReadLine();
            return name;
        }
        override public string GetProffesion(string proffesion)
        {
            Console.WriteLine("Enter proffesion: ");
            proffesion = Console.ReadLine();
            return proffesion;
        }
        override public int GetAge(int age)
        {
            Console.WriteLine("Enter age: ");
            age = Int32.Parse(Console.ReadLine());
            return age;
        }
    }
    public class engineer : cadre
    {
        private string Cname;
        private string Cproffesion;
        private int Cage;
        private int Csalary;
        public engineer()//default constr
        {
            Cname = this.GetName(Cname);
            Cproffesion = this.GetProffesion(Cproffesion);
            Cage = this.GetAge(Cage);
            Csalary = 0;
        }
        ~engineer()
        { Console.WriteLine("Destruct"); }
        public void GetSalary()//construct
        {
            Console.WriteLine("Enter salary: ");
            Csalary = Int32.Parse(Console.ReadLine());
        }
        override public void Read()//construct
        {
            Console.WriteLine("Name: " + this.Cname + '\n' + "Proffesion:" + this.Cproffesion + '\n' + "Age: " + this.Cage + '\n' + "Salaray: " + this.Csalary + '\n');
        }
        override public string GetName(string name)
        {
            Console.WriteLine("Enter name: ");
            name = Console.ReadLine();
            return name;
        }
        override public string GetProffesion(string proffesion)
        {
            Console.WriteLine("Enter proffesion: ");
            proffesion = Console.ReadLine();
            return proffesion;
        }
        override public int GetAge(int age)
        {
            Console.WriteLine("Enter age: ");
            age = Int32.Parse(Console.ReadLine());
            return age;
        }
    }
    public class administration
    {
        private worker work;
        private engineer engi;
        public administration()//default constr
        {
            work = new worker();
            engi = new engineer();
        }
        public void GetSalaryWork()//construct
        {
            work.GetSalary();
        }
        public void GetSalaryEngi()//construct
        {
            engi.GetSalary();
        }
        public void Show()//construct
        {
            engi.Show();
            work.Show();
        }
        ~administration()
        { }
    }
    public class TestMain
    {
        public static void Main()
        {
            List<Part> parts = new List<Part>();
            administration admin = new administration();
            parts.Add(new Part() { admin });
            parts.Add(new Part() { });
            parts.Add(new Part() { });

            //one.Show();
            //one.GetSalaryEngi();
            //one.GetSalaryWork();
            //one.Show();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要指定要设置的属性:

parts.Add(new Part() { admin = admin });