I have class which represents every object in my simple game (player, enemy, beam etc - they all have many commons like speed, position, dmg). So i made class named Thing. Here is how it looks like:
I have extended the class Player:
How to: Define Abstract Properties (C# Programming Guide); 2 minutes to read; Contributors. All; In this article. The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors - it declares that the class supports properties, but leaves the accessor implementation to derived classes.
And here is the problem because my IDE underlines x, y and speed fields red and tells they cannot be accessed from Player class. I tried to change them into private and default but there appears an error after that. What am I doing wrong? When i create new object from class which extends Thing I want to copy all fields and init them as it is said in constructor. So how to repair it?
BalusC876k309309 gold badges32323232 silver badges32743274 bronze badges
Michał TaborMichał Tabor1,51144 gold badges1313 silver badges2828 bronze badges
2 Answers
You need to use
getX()
, getY()
etc., because x
,y
, speed
are private
variables for class Thing
.The fact that
kosakosaPlayer extends Thing
doesn't mean Player can access private
fields. Thing
provided public
get... set...
to access its private
variables.59.7k99 gold badges109109 silver badges147147 bronze badges
Change the variables
x
, y
, and speed
to protected, or use the accessors getX()
, getY()
, getSpeed()
(getSpeed()
needs to be added in this case) to solve the access issues.The error that appeared after you changed them to default was the fact that you're calling
MikeMikeabs(...)
instead of Math.abs(...)
. Change all instances of abs(...)
to Math.abs(...)
to get rid of the new errors.1,95611 gold badge1212 silver badges1919 bronze badges
![Constructor Constructor](/uploads/1/2/4/8/124851151/120709125.png)