Note that this is not an inheritance or interface.
I have a class HP
public class HP
{
public int Base;
public int Value
{
get
{
//Need to access Monster.Level to calculate total HP.
}
}
}
And a class Monster calling HP
public class Monster
{
public HP hp;
public int Level;
}
How can I access Monster.Level from HP?
I tried to pass Level by reference to HP upon instantiating.
public class HP
{
private ref int Level;
public A(ref i)
{
Level=i;
}
}
But I want to keep A as simple and clean as possible.
The reason I don't want to pass the level upon creation is Level is a constantly changing valuable, I'll will then have to "update" the HP every time the monster gains a level or something, which is NOT a efficient way to do things.
And there are other things accessing Level, such as Attack and Damage, etc.
I currently has a method within HP, Attack, Damage, etc., taking the Level as a parameter.
public int Value(int lv)
{
return Base+10*lv;
}
But according to the a little bit of SOLID principle I know, this is a VERY BAD coding!
It seems like what you called HP is more of a HPCalculator responsible of calculating Monster's HP based on this Moster's level and possibly other data in the future, I think it seem crucial for such HPCalculator to have access to the Monster of which health it calculates.
In C# classes are done by references not by values meaning you can in no problem have something like this:
public class Monster{
public HP hp;
}
public class HP{
public Monster monster;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With