我对 C#.NET 还是比较陌生,所以我确信我缺少一些明显的东西,但这里是:我无法从特定类中获取一些变量。
有问题的类如下所示:
class AridPlanet : Arid
{
public const int area = 95;
}
如您所见,该类继承自以下类:
abstract class Arid : Astro
{
public const int metal = 2;
public const int gas = 2;
public const int crystals = 0;
public const int fertility = 5;
}
它又继承自以下类:
abstract class Astro
{
public int metal;
public int gas;
public int crystals;
public int fertility;
public int area;
}
我尝试获取以下变量:
class Base
{
private int metal;
private int gas;
private int crystals;
private int fertility;
private int area;
private List<Structure> structures = new List<Structure>();
private int position;
private Astro selectedAstro;
public Base(Astro astro, int position)
{
this.selectedAstro = astro;
this.metal = selectedAstro.metal;
this.gas = selectedAstro.gas;
this.crystals = selectedAstro.crystals;
this.fertility = selectedAstro.fertility;
this.area = selectedAstro.area;
this.position = position;
//Code ommited
}
}
参数作为特定的astro 类型astro
传递给Base
构造函数,例如AridMoon
.
当我运行代码查看Base
变量被分配给什么时,我看到它们都被分配给 0。对我来说,这表明代码正在尝试从最顶层的超类中分配AridPlanet
变量Astro
。
但是,据我了解继承,编译器应该先查看指定的类,然后再转到超类。
对出了什么问题有任何想法吗?我确定这是我误解的简单事情。