简体   繁体   English

嵌套类? 属性与属性?

[英]c# - Nested classes? Properties with properties?

I would like to create a class with properties that have subproperties... 我想创建一个具有子属性的类...

In other words, if I did something like: 换句话说,如果我做了类似的事情:

Fruit apple = new Fruit();

I would want something like: 我想要类似的东西:

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

I would think it would be done like: 我认为这样做可以:

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

}

...but, if that were working, I wouldn't be on here... ...但是,如果那行得通,我就不会在这里...

So close! 很近! Here's how your code should read: 您的代码应如下所示:

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

A class just defines the signature, but nested classes don't automatically become properties. 一个类仅定义签名,但是嵌套的类不会自动成为属性。

As a side note, there's also a couple things I'd recommend, to follow .NET's "best practices": 作为附带说明,我还建议您遵循.NET的“最佳做法”:

  • Don't nest classes unless necessary 除非必要,否则不要嵌套类
  • All Public members should be Properties instead of Fields 所有公共成员应为“属性”而不是“字段”
  • All Public members should be PascalCase, not camelCase. 所有Public成员应该是PascalCase,而不是camelCase。

I'm sure there's plenty of documentation on best practices too, if you're so inclined. 我敢肯定,如果您愿意的话,也有很多关于最佳做法的文档。

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

Can you extrapolate the class, then reference it? 您可以外推课程,然后引用它吗?

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

}

public class physicalProperties{
        public int height = 3;
    }
class Fruit{
    public class physicalProperties{
        public int height = 3;
    }
}

Fruit.physicalProperties does indeed declare another type, but: Fruit.physicalProperties确实确实声明了另一种类型,但是:

  1. It will be private, only available to the members of Fruit unless you declare it public . 它将是私有的,除非您声明为public否则只有Fruit成员才能使用。
  2. Creating a type does not create an instance of that type, or member of Fruit to allow users of Fruit to access it. 创建类型不会创建该类型的实例,也不会创建Fruit成员以允许Fruit用户访问它。

You need to add: 您需要添加:

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

and in the (default) constructor of Fruit assign PhysicalProperties an instance of physicalProperties . 并在Fruit的(默认)构造函数中为PhysicalProperties分配一个physicalProperties实例。

You will need to expose the property as it is contained for the instance of the class physicalProperties so maybe you could do like 您将需要公开该属性,因为它包含在类physicalProperties的实例中,所以您可能会喜欢

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

And a property which gives it back 还有可以归还的财产

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

OR 要么

public physicalProperties physical;

so that you can do apple.physical.height 这样你就可以做apple.physical.height

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

You can then access the sub-properties like so: 然后,您可以像这样访问子属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM