public class City
{
public class Population
{
public int number;
public void change(int amount)
{
number = amount;
}
public class Buildings
{...}
public class Military
{...}
}
让我们说在另一个班级,我有
public City[] PlayerCities = new City[6];
如果我尝试像PlayerCities[0].Population.Change(4)
这样的内容,它就无法正常工作。
答案 0 :(得分:0)
您需要先创建Population
对象,然后才能访问其方法。另一种方法是将Change变为静态变量(在您的情况下没有意义,但重要的是要知道)。
在下面的代码中,我在Population
City
的自动属性
public class City
{
public Population Population {get; set;}
public class Population
{
public int number;
public void change(int amount)
{
number = amount;
}
public class Buildings
{...}
public class Military
{...}
}
添加后,您可以执行PlayerCities[0].Population.Change(4)