我有一個主要的類有一些公共常量變量,我有一個自定義類,我想知道如何從自定義類訪問主類的常量?如何從子類訪問主類的公共常量變量?
主類代碼:
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