【发布时间】: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