将类变量传递给另一个类
我想将一个类变量传递给另一个类,并使其成为该类的类变量。在以下情况下我将如何执行此操作?
public class GLCamTest extends Activity {
public float array[] = something;
}
class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback,
Camera.PreviewCallback, Renderer {
//Get class variable here
}
I'd like to pass a class variable to another class and make it a class variable of that class. How would I do this in the following context?
public class GLCamTest extends Activity {
public float array[] = something;
}
class GLLayer extends GLSurfaceView implements SurfaceHolder.Callback,
Camera.PreviewCallback, Renderer {
//Get class variable here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很难理解你问的是什么,但这里有一个可能的答案:
使类 B 成为 A 的子类:
如果
array
被重新声明为public static
,你会得到一个存在可称为A.array
或B.array
的array
属性的情况。 (或者在A
或B
中作为array
...甚至作为a.array
或b.array
其中a
和b
分别具有类型A
和B
。)如果您无法在
A
和B
(或A
、B
和包含声明)那么你就不走运了。他们无法共享声明。但是,您可以使用静态导入来使声明看起来像是共享的。例如:
顺便说一句,使用静态变量来共享状态通常是一个坏主意,尤其是表示为裸数组的状态。这显然是非面向对象的。
It is difficult to understand wjat you are asking, but here's a possible answer:
Make class B a subclass of A:
If
array
is redeclared to bepublic static
, you get a situation where there is anarray
attribute that can be referred to asA.array
orB.array
. (Or within eitherA
orB
as justarray
... or even asa.array
orb.array
wherea
andb
have typesA
andB
respectively.)If you cannot create a direct or subtype relationship between
A
andB
(orA
,B
and some third class containing the declarations) then you are out of luck. There is no way that they can share declarations.However, you can use static imports to make it seem like the declaration is shared. For example:
Incidentally, it is generally a bad idea to use
static
variables to share state, especially state represented as bare arrays. This is distinctly non-object-oriented.您有权访问 A 的实例吗?或者也许您希望数组是静态的?
Do you have access to instance of A? Or maybe you want array to be static?
您是否希望数组在任何地方都可见(就像它是一个全局变量一样)?
如果是这样,那么它需要是静态的。
但我的猜测是,您希望将 GLCamTest 的实例传递给 GLLayer 对象,在这种情况下,您应该使用 setter 函数或在构造函数中传递它。
Do you want the array to be visible everywhere (as if it was a global variable)?
If so then it needs to be static.
But my guess is that you want to pass an instance of GLCamTest on to a GLLayer object, in which case you should use a setter function or pass it in the constructor.