私はまだ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
。
ただし、継承について理解したように、コンパイラは、スーパークラスに移動する前に、最初に示されたクラスを確認する必要があります。
何が悪いのかについて何か考えはありますか?私が誤解しているのは単純なことだと確信しています。