2010-01-25 79 views
4

我有一個抽象類Employee和其他兩個類來擴展它(Developer和Manager)。我的問題是,當我每當我創建一個經理當傳遞給C#中的屬性時,對象變爲空。

Employee man = new Manager(1234567, 30, "Bob", "Pie") 

,並嘗試將其設置在一個新開發的管理領域,

Employee codemonkey = new Developer(1234568, 20, "Code", "Monkey", (Manager)man) 

我不斷收到的ArgumentException,我的經理爲空。我做了一些檢查,當我嘗試在構造函數中使用Manager屬性進行設置時,它會以某種方式變爲空。任何意見,爲什麼我得到這個錯誤將不勝感激。 TIA!

每個的代碼如下:

// Employee類

public abstract class Employee 
{ 
    string firstName, lastName; 
    int id, yearsEmployed; 

    //Names must be non-empty 
    public string FirstName 
    { 
     get { return firstName; } 
     set 
     { 
      if (!value.Equals("")) 
      { 
       firstName = value; 
      } 
      else 
       throw new ArgumentException("name cannot be empty"); 
     } 
    } 
    public string LastName 
    { 
     get { return lastName; } 
     set 
     { 
      if (!value.Equals("")) 
      { 
       lastName = value; 
      } 
      else 
       throw new ArgumentException("name cannot be empty"); 
     } 
    } 
    // IDs must be strings consisting of exactly seven digits. 
    public int ID 
    { 
     get { return id; } 
     set 
     { 
      if (value.ToString().Length == 7) 
      { 
       id = value; 
      } 
      else 
       throw new ArgumentException("ID must consist of 7 digits"); 
     } 
    } 
    // Years employed must always be non-negative. 
    public int YearsEmployed 
    { 
     get { return yearsEmployed; } 
     set 
     { 
      if (value >= 0) 
      { 
       yearsEmployed = value; 
      } 
      else 
       throw new ArgumentException("Year employed must be non-negative"); 
     } 
    } 
    //Constructor 
    public Employee(int id, int yearsEmployed, 
        string firstName, string lastName) 
    { 
     this.FirstName = firstName; 
     this.LastName = lastName; 
     this.ID = id; 
     this.YearsEmployed = yearsEmployed; 
    } 
    public abstract int GetLevel { get; } 
    public abstract string GetTitle { get; } 
    public string GetFullTitle { get { return GetTitle + " " + GetLevel; } } 
} 

//開發類:

public class Developer : Employee 
{ 
    Manager manager; 

    //Manager cannot be null 
    public Manager Manager 
    { 
     get { return manager; } 
     set 
     { 
      if (manager != null) 
      { 
       manager = value; 
      } 
      else 
       throw new ArgumentException("Manager cannot be null"); 
     } 
    } 

    //Constructor 
    public Developer(int id, int yearsEmployed, string firstName, 
        string lastName, Manager manager) 
     : base(id, yearsEmployed, firstName, lastName) 
    { 
     Console.WriteLine("manager is not null:" + manager != null); //True here 
     this.Manager = manager; // manager is null here 
    } 

    public override int GetLevel 
    { 
     get { return (this.YearsEmployed + 1)/3; } 
    } 

    public override string GetTitle 
    { 
     get { return "Developer"; } 
    } 
} 

// Manager類

public class Manager : Employee 
{ 
    //Constructor 
    public Manager(int id, int yearsEmployed, 
        string firstName, string lastName) 
     : base(id, yearsEmployed, firstName, lastName) { } 

    public override int GetLevel 
    { 
     get { return (YearsEmployed + 1)/2; } 
    } 

    public override string GetTitle 
    { 
     get { return "Manager"; } 
    } 
} 
+4

+1了在給他們打電話時給開發者一些幫助代碼猴子 – Marcelo 2010-01-25 16:57:54

+0

有趣。我甚至沒有注意到這一點。我的大腦跳過了代碼。我想,讓我成爲代碼猴。 – 2010-01-25 17:09:26

+0

我一直認爲代碼猴是一個可愛的術語。我想不是每個人都這樣看待它。如果冒犯了任何人,我的道歉。 – Mel 2010-01-25 17:36:50

回答

9

唐你不想說:

if (value != null) 

代替

if (manager != null) 

經理字段將被初始化爲null。 value關鍵字表示正在傳遞給該屬性的數據。

1

你永遠不會設置字段管理器的值,只有屬性管理器,所以在你檢查管理器的值時它的屬性值是空的,因爲它沒有被設置。你可以設置現場經理在構造函數中:

this.manager=manager 

,並在屬性檢查值

if (value!=null) 
{ 
    manager =value; 
} 

(你需要無論如何要做到這一點,否則你給價值的財產是永遠使用並將永遠不會更新)

取決於您是否希望Manager能夠被更改。

2

變化

if (manager != null) 

if (value != null) 
1

在Developer.Manager的setter變化

if (manager != null) 

if (value != null) 
相關問題