2015-04-04 188 views
0

我需要創建類的對象列表,但會引發錯誤! 錯誤1無法使用集合初始值設定項初始化類型'Lab_5_class_console.Part',因爲它沒有實現'System.Collections.IEnumerable' 線部分的錯誤.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(); 

     } 
    } 
} 
+0

在行錯誤parts.Add(新部分(){管理}); – MoNZ 2015-04-04 13:35:28

+0

在你的問題中加入這個。 – serenesat 2015-04-04 14:52:59

回答

1

你需要指定的屬性,你想設置:

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

更喜歡'admin = admin' – Banana 2015-04-04 13:37:47

+0

parts.Add(new Part(){administration = admin}); 新錯誤 - 錯誤'Lab_5_class_console.Part'沒有包含'administration'的定義 – MoNZ 2015-04-04 13:40:03

+0

@MoNZ編輯修復錯誤 – nvoigt 2015-04-04 14:01:53

相關問題