如何从子类访问主类的公共常量?
我有一个带有几个公共常量变量的主类,并且我有一个自定义类,我想知道如何从自定义类访问主类的常量?
主类代码:
import processing.core.*;
import toxi.geom.*;
import toxi.math.*;
public class VoronoiTest extends PApplet {
// this are the constants I want to access from the Site class
public static int NUM_SITES = 8;
public static int SITE_MAX_VEL = 2;
public static int SITE_MARKER_SIZE = 6;
Site[] sites;
public void setup() {
size( 400, 400 );
sites = new Site[NUM_SITES];
for ( int i = 0; i < sites.length; i++) {
sites[i] = new Site( this );
}
}
}
这是站点类代码:
import processing.core.*;
public class Site {
PApplet parent;
float x, y;
PVector vel;
int c;
Site ( PApplet p ) {
parent = p;
// here I try to get the constants from the main class
vel = new PVector( parent.random(-parent.SITE_MAX_VEL, SITE_MAX_VEL), parent.random(-SITE_MAX_VEL, SITE_MAX_VEL) );
}
}
任何帮助将不胜感激!
I have a main class with a couple of public constant variables, and I have a custom class, I would like to know how can I access the constants of the main class from the custom class?
Main Class code:
import processing.core.*;
import toxi.geom.*;
import toxi.math.*;
public class VoronoiTest extends PApplet {
// this are the constants I want to access from the Site class
public static int NUM_SITES = 8;
public static int SITE_MAX_VEL = 2;
public static int SITE_MARKER_SIZE = 6;
Site[] sites;
public void setup() {
size( 400, 400 );
sites = new Site[NUM_SITES];
for ( int i = 0; i < sites.length; i++) {
sites[i] = new Site( this );
}
}
}
And this is the Site class code:
import processing.core.*;
public class Site {
PApplet parent;
float x, y;
PVector vel;
int c;
Site ( PApplet p ) {
parent = p;
// here I try to get the constants from the main class
vel = new PVector( parent.random(-parent.SITE_MAX_VEL, SITE_MAX_VEL), parent.random(-SITE_MAX_VEL, SITE_MAX_VEL) );
}
}
Any help will be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能。由于
parent
的类型为PApplet
,而不是VoronoiTest
,因此您无法保证它具有静态成员 SITE_MAX_VEL。相反,如果
parent
的类型为VoronoiTest
,则通过实例访问静态变量就没有什么意义,因为不可能它要改变。如前所述,要访问静态成员,请使用
ClassName.STATIC_MEMBER
表示法(在本例中为VoronoiTest.SITE_MAX_VEL
)。更好的是,只需将常量存储在
Site
类中即可。毕竟,这对他们来说似乎是最合乎逻辑的地方。You can't. Because
parent
is of typePApplet
, notVoronoiTest
, you cannot guarantee that it has the static member SITE_MAX_VEL.Conversely, if
parent
were of typeVoronoiTest
, there would be little point in accessing the static variable through the instance, as it would be impossible for it to change.As already mentioned, to access static members, use the
ClassName.STATIC_MEMBER
notation (in this case,VoronoiTest.SITE_MAX_VEL
).Better yet though, just store the constants in the
Site
class instead. After all, that seems the most logical place for them.使用 VoronoiTest 参考。例如,VoronoiTest.SITE_MAX_VEL。当您使用 PApplet 引用时,编译器没有任何方法知道静态变量是否存在。
Use the VoronoiTest reference. VoronoiTest.SITE_MAX_VEL, for example. When you use a PApplet reference, the compiler doesn't have any way of knowing that the static variables exist.
静态字段通过类名访问。使用
VoronoiTest.SITE_MAX_VEL
。Static fields are accessed via the class-name. Use
VoronoiTest.SITE_MAX_VEL
.