我有一个主要的类有一些公共常量变量,我有一个自定义类,我想知道如何从自定义类访问主类的常量?如何从子类访问主类的公共常量变量?
主类代码:
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));
}
}
任何帮助将非常感激!
网站不是VoronoiTest的子类。 – 2011-04-02 12:12:10