问题
I've got a custom class called 'Quad' which creates a textured quad to use as a sprite in my 2D OpenGL ES 2.0 game.
public class Quad(){
//Quad creation stuff here
}
Then I have a separate subclass (i.e. in a different file - not an innerclass)
public class hero extends Quad(){
//Variables relating specifically to this character
int heroX = 0;
int heroY = 0;
}
I create my object like so:
Quad hero = new Hero();
However, if I attempt to access the 'heroX' and 'heroY' variables, I get nothing.....
So I'll try
hero.heroX
but the above doesn't pick this variable up.
It will. however, find variables that are located in my Quad class. But I would have thought that by extending my Quad class into my Hero class, they would be available through my object.
Could someone please explain where my thinking is going wrong and how I can access these 2 variables? Thanks
回答1:
Your variable hero
is of type Quad
, not type Hero
, hence the compiler only knows that it is a Quad
. Use the specific subtype if you want access to Hero
methods.
来源:https://stackoverflow.com/questions/16267433/not-able-to-access-object-sub-classs-variable-java-android