不能访问对象的子类的变量? (Jav​​a / Android版) [英] Not able to access object sub class's variable? (Java / Android)

查看:84
本文介绍了不能访问对象的子类的变量? (Jav​​a / Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有所谓的'四',这将创建一个纹理四在我的2D OpenGL ES 2.0的游戏精灵使用自定义类。

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();

不过,如果我尝试访问heroX'和'海略变量,我什么也得不到......

However, if I attempt to access the 'heroX' and 'heroY' variables, I get nothing.....

所以我会尽力

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.

可能有人请解释一下在我的想法是怎么了,我怎么能访问这些2个变量?谢谢

Could someone please explain where my thinking is going wrong and how I can access these 2 variables? Thanks

推荐答案

您变量英雄的类型为不键入英雄,因此编译器只知道它是一个。如果你想获得英雄方法使用的具体亚型。

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.

这篇关于不能访问对象的子类的变量? (Jav​​a / Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆