c#-嵌套类?属性与属性?
我想创建一个具有子属性的属性的类 换句话说,如果我做了如下事情:c#-嵌套类?属性与属性?,c#,class,nested,C#,Class,Nested,我想创建一个具有子属性的属性的类 换句话说,如果我做了如下事情: 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 我想要像这样的东西: Fruit ap
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
我想要像这样的东西:
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;
}
}
…但是,如果那样做有效,我就不会在这里了…太近了!下面是您的代码的阅读方式:
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的“最佳实践”:
- 除非必要,否则不要嵌套类
- 所有公共成员都应该是属性而不是字段
- 所有公众成员都应该是PascalCase,而不是camelCase
如果你愿意的话,我相信也有很多关于最佳实践的文档。你能推断出这个类,然后引用它吗
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;
}
class Fruit{
public void eat(){
MessageBox.Show("Fruit eaten");
}
public string color = "red";
public physicalProperties{
height = 3;
}
}
public class physicalProperties{
public int height = 3;
}
Fruit.physicalProperties
确实声明了另一种类型,但是:
Fruit
的成员才能使用,除非您将其声明为public
Fruit
的成员以允许Fruit
的用户访问该类型class Fruit {
private physicalProperties props;
public physicalProperties PhysicalProperties { get; private set; }
在
Fruit
的(默认)构造函数中,分配PhysicalProperties
一个PhysicalProperties
的实例,您需要公开该属性,因为它包含在类PhysicalProperties
的实例中,所以您可以这样做
public Fruit()
{
physical = new physicalProperties();
}
还有一个能把它还给你的财产
public int Height { get { return 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();
}
然后可以访问子属性,如下所示:
Fruit apple = new Fruit();
MessageBox.Show(apple.PhysicalProperties.height.ToString());
那么蝙蝠侠又有什么问题呢?但是,如果那样的话,我就不会在这里了…
apple
现在没有physicalProperties
,只有Fruit
有。现在它只是一种类型。您肯定无法访问它的高度
,因为物理属性
没有高度
,只有它的实例有。LOL!还有人注意到这个例子中苹果的拟人化吗?i、 e.“苹果什么时候可以吃东西?”:P你不需要创建一个实例吗?否则高度将不可访问。如果我不应该嵌套类,我如何向属性添加属性?@Walkemeo嵌套类只是指名称空间。。。我甚至不应该提起它,这更像是一种偏好。阅读更多信息:@walkereno-您可以在“Fruit”类之外定义该类,但仍然可以在Fruit类中创建该类的实例。正如String在别处定义的一样,但在Fruit类中仍然有一个String实例。另外,如果我希望该属性在没有调用其成员时返回值,该怎么办?这应该为height@V4Vendetta的确如此!现在怎么办?检查我的答案,在字段的构造函数中实例化需要初始化的字段,否则将得到null引用
异常<代码>公共物理属性物理=新物理属性()代码>