2010-01-25 91 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) 
相关问题