Reputation: 1373
I'm still relatively new to C#.NET so I'm sure that there is something obvious that I'm missing but here it is: I'm having trouble getting some variables out of a particular class.
The class in question looks like this:
class AridPlanet : Arid
{
public const int area = 95;
}
As you can see this class inherits from the following class:
abstract class Arid : Astro
{
public const int metal = 2;
public const int gas = 2;
public const int crystals = 0;
public const int fertility = 5;
}
Which in turn inherits from the following class:
abstract class Astro
{
public int metal;
public int gas;
public int crystals;
public int fertility;
public int area;
}
I attempt to get the variables in the following:
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
}
}
The astro
parameter is passed to the Base
constructor as a specific astro type, for example AridMoon
.
When I run the code to see what the Base
variables are assigned to I see that they are all assigned to 0. To me, this suggests that the code is trying to assign the variables from the top most superclass of AridPlanet
which is Astro
.
However, as I understood inheritance, the compiler should look at the class indicated first before moving on to superclasses.
Any thoughts on what is going wrong? I'm sure that it's something simple that I've misunderstood.
Upvotes: 0
Views: 157
Reputation: 955
I think this is what you should do:
class AridPlanet : Arid
{
public AridPlanet()
{
area = 95;
}
}
abstract class Arid : Astro
{
public Arid()
{
metal = 2;
gas = 2;
crystals = 0;
fertility = 5;
}
}
Then change the Astro class fields access modifier to protected and create respective readonly properties with public access.
EDIT:
abstract class Astro
{
protected int metal;
protected int gas;
protected int crystals;
protected int fertility;
protected int area;
public int Metal { get { return metal; } }
public int Gas { get { return gas; } }
public int Crystals { get { return crystals; } }
public int Fertility { get { return fertility; } }
public int Area { get { return area; } }
}
class BaseCalc
{
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 BaseCalc(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
}
}
Upvotes: 1
Reputation: 2168
Let me use an example here to illustrate what you do:
It looks as if you were looking to use polymorphism. Use an interface or abstract class (as Eric showed) as base class that determines what information and capability each derived class must have. When you create a new instance of your subtype you can store it as the type your interface is and all calls to the instance will be delegated to the subclass. The important part is, which constructor you call.
Upvotes: 1
Reputation: 150178
If you reference Astro astro, you will in fact get the constants as defined in Astro. You would have to cast it to a specific subclass to get the constants defined in that subclass.
What you are probably looking for is virtual properties.
You can define
abstract class Base
{
abstract public int Metal { get; }
}
Then you can implement that abstract method in a subclass
public class AridPlanet : Base
{
const int ARID_PLANET_METAL = 2;
public override int Metal { get { return ARID_PLANET_METAL; } }
}
I skipped over intermediate levels of your hierarchy, but hopefully it illustrates the point. Intermediate classes can override the abstract definition. Their subclasses can optionally supply an additional override or accept the one from the intermediate class.
Upvotes: 2