类成员如何获取类对象实例
class Company
{
List<Department> deptList;
private List<Department> DeptList
{
get { return deptList; }
set { deptList = value; }
}
}
class Department
{
private Company parent;
internal Company Parent
{
get
{
//这里怎么写才能获得当前Company的对象实例???
}
//set { parent = value; }
}
}
------解决方案--------------------class Department
{
private Company _parent;
public Department(Company parent)
{
this._parent = parent;
}
internal Company Parent
{
get
{ return this._parent;}
//set { _parent = value; }
}
}
在Company类中,给List<Department>赋值时使用带参的构造函数来New Department(this)
如果Department的无参构造是必需的,使用Department.Parent时就判断下是否为null
------解决方案--------------------真要较真的话就做成静态的
class Company
{
private static Company _Company;
public static Company Company
{
get;
}
List<Department> deptList;
private List<Department> DeptList
{
get { return deptList; }
set { deptList =