创建主类的对象,该对象依次实例化其他成员类

时间:2014-10-09 12:31:21

标签: c# asp.net .net

有没有办法我们只能创建包含其他类的Main类的对象。

 public class City
{
    public int CityId { get; set; }
    public string CItyName { get; set; }
}
  public class State
{
    public int StateId { get; set; }
    public string StateName { get; set; }
}`
 public class CurrentPresident
{
    public int PresidentId { get; set; }
    public string PresidentName { get; set; }
}
 public class Country
{
    public int CountryId { get; set; }
    public CurrentPresident President { get; set; }
    public IList<State> States { get; set; }
    public IList<City> Cities { get; set; }
}

我有四个不同的班级,而我的国家班级则将不同的班级作为成员 现在我想创建Country类的对象,而不是Country类中的其他类。

Country country=new Country();

为了更清楚,我不想创建City,State和CurrentPresident类的对象。我只想创建Country Class的对象,而Country对象也实例化Country类中的其他类。

非常感谢

4 个答案:

答案 0 :(得分:1)

虽然不确定原因,但如果您不想在创建Country时创建其他对象,请让Country内的getter返回一个私有字段并启动您的字段吸气。这样,只有在您第一次访问属性时才会创建属性,而不是在Country的ctor中创建属性,因为它们是在您访问它们时创建的,所以您不需要在Country之外创建它们。

public class Country
{
    ....
    public CurrentPresident President 
    { 
        get
        {
            if (_president == null)
            {
                _president = new CurrentPresident();
            }
            return _president;
        } 
        //no setter as outside objects don't need to create them
    }
    ....

    private CurrentPresident _president
}

答案 1 :(得分:0)

您的国家/地区类应该如下

public class Country
{
    public int CountryId { get; set; }
    public CurrentPresident President { get; set; }
    public IList<State> States { get; set; }
    public IList<City> Cities { get; set; }
    public Country()
    {
          States = new List<State>();
          Cities =new List<City>();
    }
}

当您使用对象初始化时,它不会起作用。你需要使用一些技巧。

答案 2 :(得分:0)

据我所知,您想创建一个国家/地区,但不填充该类的内容。换句话说,您希望国家/地区为空,并在创建时可用。

您可以使用构造函数来实现此目的:

public class Country
{
    public int CountryId { get; set; }
    public CurrentPresident President { get; set; }
    public IList<State> States { get; set; }
    public IList<City> Cities { get; set; }

    public Country()
    {
       this.CountryId = 0;
       this.CurrentPresident = null; // Country is brand new, no president elected :-)
       this.States = new List<State>();
       this.Cities = new List<City>();
    }
}

答案 3 :(得分:0)

您必须在主对象构造函数中实例化所有内部对象。例如:

public class City
{
    public int CityId { get; set; }
    public string CItyName { get; set; }
}
public class State
{
    public int StateId { get; set; }
    public string StateName { get; set; }
}
public class CurrentPresident
{
    public int PresidentId { get; set; }
    public string PresidentName { get; set; }
}
public class Country
{
    public Country()
    {
        this.President = new CurrentPresident();
        this.States = new List<State>();
        this.Cities = new List<City>();
    }

    public int CountryId { get; set; }
    public CurrentPresident President { get; set; }
    public IList<State> States { get; set; }
    public IList<City> Cities { get; set; }
}

请记住,尽管每个对象都存在,但其中的所有对象仍然是空的(没有PresidentId,空List等等。)

您可能希望根据需要创建具有不同参数的不同构造函数。例如,您可能希望在创建CurrentPresident时提供Country,因此它始终包含有效数据。