【问题标题】:c# - Nested classes? Properties with properties?c# - 嵌套类?有属性的属性?
【发布时间】:2011-12-23 08:59:59
【问题描述】:

我想创建一个具有子属性的类...

换句话说,如果我做了类似的事情:

Fruit apple = new Fruit();

我想要类似的东西:

apple.eat(); //eats apple
MessageBox.Show(apple.color); //message box showing "red"
MessageBox.Show(apple.physicalProperties.height.ToString()); // message box saying 3

我想应该是这样的:

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public class physicalProperties{
        public int height = 3;
    }

}

...但是,如果那行得通,我就不会在这里...

【问题讨论】:

  • 那么蝙蝠侠有什么问题?
  • ..但是,如果那行得通,我就不会在这里了...
  • apple 现在没有physicalProperties,只有Fruit 有。现在只是一种类型。而且你绝对不能访问它的height,因为physicalProperties 没有height,只有它的实例有。
  • 哈哈!有没有其他人注意到这个例子中苹果的拟人化?即“苹果什么时候可以吃东西?” :P

标签: c# class nested


【解决方案1】:

如此接近!以下是您的代码的阅读方式:

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public class PhysicalProperties{
        public int height = 3;
    }
    // Add a field to hold the physicalProperties:
    public PhysicalProperties physicalProperties = new PhysicalProperties();
}

类只是定义签名,但嵌套类不会自动成为属性。

作为旁注,我还建议遵循 .NET 的“最佳实践”:

  • 除非必要,否则不要嵌套类
  • 所有公共成员都应该是属性而不是字段
  • 所有 Public 成员都应该是 PascalCase,而不是 camelCase。

如果您愿意的话,我相信也有大量关于最佳实践的文档。

【讨论】:

  • 你不需要创建一个实例,否则高度将无法访问
  • 如果我不应该嵌套类,如何向属性添加属性?
  • @Walkemeo 嵌套类只是指命名空间......我什至不应该提起它,它更像是一种偏好。在这里阅读更多:stackoverflow.com/questions/804453/…
  • @Walkerneo - 您可以在“Fruit”类之外定义类,但仍然在 Fruit 类中创建它的实例。就像 String 是在其他地方定义的一样,但在 Fruit 类中仍然有一个 String 实例。
  • 另外,如果我希望该属性在没有任何成员被调用的情况下返回一个值怎么办?
【解决方案2】:
class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    //you have declared the class but you havent used this class
    public class physicalProperties{
        public int height = 3;

    }
    //create a property here of type PhysicalProperties
    public physicalProperties physical;
}

【讨论】:

  • 这应该给高度一个空引用异常
  • @V4Vendetta,确实如此!现在呢?
  • 检查我的答案,在 Fruit 的构造函数中实例化
  • 该字段需要初始化,否则你会得到null-reference 异常。 public physicalProperties physical = new physicalProperties();
【解决方案3】:

你能推断出类,然后引用它吗?

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public physicalProperties{
        height = 3;
    }

}

public class physicalProperties{
        public int height = 3;
    }

【讨论】:

    【解决方案4】:
    class Fruit{
        public class physicalProperties{
            public int height = 3;
        }
    }
    

    Fruit.physicalProperties 确实声明了另一种类型,但是:

    1. 它将是私有的,仅对Fruit 的成员可用,除非您将其声明为public
    2. 创建类型不会创建该类型的实例或Fruit 的成员以允许Fruit 的用户访问它。

    你需要添加:

    class Fruit {
      private physicalProperties props;
      public physicalProperties PhysicalProperties { get; private set; }
    

    并在Fruit 的(默认)构造函数中为PhysicalProperties 分配physicalProperties 的实例。

    【讨论】:

      【解决方案5】:

      您需要公开该属性,因为它包含在 physicalProperties 类的实例中,所以也许您可以这样做

      public Fruit()
      {
          physical = new physicalProperties();
      }
      

      还有一个将它还给它的属性

      public int Height { get { return physical.height;}}
      

      public physicalProperties physical;
      

      这样你就可以apple.physical.height

      【讨论】:

        【解决方案6】:
        class Fruit{
            public void eat(){
                MessageBox.Show("Fruit eaten");
            }
            public string color = "red";
            public class physicalProperties{
                public int height = 3;
            }
            // create a new instance of the class so its public members
            // can be accessed
            public physicalProperties PhysicalProperties = new physicalProperties();
        }
        

        然后您可以像这样访问子属性:

        Fruit apple = new Fruit();
        MessageBox.Show(apple.PhysicalProperties.height.ToString());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-09
          • 2015-10-25
          • 2023-04-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多