【发布时间】:2012-07-01 22:56:22
【问题描述】:
在我正在开发的一款游戏中,一个类负责绘制精灵,而另一个类负责操作坐标(即,如果精灵即将离开屏幕,请将其停在那里。是的,我知道这是矢量图形)。无论如何,我需要在两个类之间共享坐标。我曾尝试使用类扩展来执行此操作,但这会产生堆栈溢出,因为超类(绘制精灵的类)必须创建子类(将要操作变量的类)的实例。
// In the superclass Render
public AnimateMC mc = new AnimateMC( 2 ); // AnimateMC is the subclass of the
superclass, Render
//later in the program
//code for drawing all of the shapes in the sprite
mc.gravitizeY(); // this is why I need to make an instance of AnimateMC
// In the subclass AnimateMC
// animates sprite by changing the coordinates
如何在不扩展一个类的情况下在两个类之间共享整数变量坐标。
【问题讨论】:
-
getter 和 setter 有什么问题?从您的示例中并不完全清楚,但是您几乎可以肯定地过度考虑了这个问题-这是一个非常常见的问题。你能发布一个更详细的例子吗?
-
我没有介绍 getter 和 setter。我还是个高中生。那些是什么?
-
很简单的想法,一个对象(不是类,小心,你使用的术语很重要)拥有坐标,并且有一个
getCoordinates()方法可以调用并传递给第二个对象。
标签: java inheritance subclass superclass