Not able to access object sub class's variable? (Java / Android)

為{幸葍}努か 提交于 2020-01-05 10:16:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!