This is a pretty newbie question, but for some reason I could not find an answer to this online.
Let’s say I have three classes: Entities, Player, and Enemy.
Entities has a variable, health. Both Player and Enemy are child classes to Entities. How could I get health in both classes, and have it so Player has 100 and Enemy has 50?
Entities has a variable, health
Variable sounds like a field to me, please consider using properties
How could I get health in both classes
Check out accessibility modifiers (if you are using a field, only one is correct!)
How could I have it so Player has 100 and Enemy has 50?
Check out constructors.
If intentionally not giving you the exact solution, so you have a chance to learn how those things work. If you get stuck, feel free to ask 😊
Thanks! ATM I’ve got health set as protected, but I also tried public.
Hey post your code so far, so we can see where you’re at. But for the most part, any beginner inheritance lesson should cover using variables from one class to the inherited class.
Yeah I haven’t checked any inheritance lessons yet, I’ll do that tomorrow. I’m sure they’ll have the answers I need.
Thank you so much! It looks so simple, I’m amazed!
May I ask, if I was to modify Health within either Player or Enemy, would that be done within public Class()?
C# devs