【问题标题】:Create object of main class which in turns instantiate its other member classes创建主类的对象,然后实例化其其他成员类
【发布时间】:2014-10-09 12:31:21
【问题描述】:

有什么方法可以创建仅包含其他类的 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 country=new Country();

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

非常感谢

【问题讨论】:

  • 您已经发布了解决方案:Country country=new Country();。如果您还想初始化其他类型,则应提供适当的构造函数或内联初始化。
  • @Tim 感谢您的快速回复,但我的声明只创建 Country 类的对象,而不是 Country 类中的其他类。
  • 如果我把这行 Country count = new Country(); count.President.PresidentId = 1;因为没有实例化总统,我会得到错误。
  • 你总是可以在你的类中提供构造函数,以你想要的方式初始化字段。所以你可以提供一个空的构造函数(public Country(){States=new List&lt;State&gt;();CurrentPresident=new President();...}),你可以根据需要在其中初始化对象。
  • 访问尚未定义的总统毫无意义。因此,恕我直言,为每个国家/地区提供默认总统甚至是一个错误。如果你想知道一个国家的总统,你必须先指定它。例如:c.President = new CurrentPresident{ PresidentId = 1, PresidentName = "Obama" };

标签: c# asp.net .net


【解决方案1】:

虽然不确定原因,但如果您不想在创建 Country 时创建其他对象,请让 Country 内的 getter 返回一个私有字段并在您的 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
}

【讨论】:

  • 这似乎工作正常..仍然需要更多测试。非常感谢artm。
【解决方案2】:

您的国家/地区等级应遵循

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>();
    }
}

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

【讨论】:

    【解决方案3】:

    据我了解,您想创建一个 Country,但不填充课程的内容。换句话说,您希望国家/地区为空,并且在创建时可用。

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

    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>();
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      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,空的Lists 等)。

      您可能希望创建具有不同参数的不同构造函数,具体取决于您的需要。例如,您可能希望在创建 Country 时提供 CurrentPresident,以便它始终具有有效数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 2017-04-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-13
        相关资源
        最近更新 更多