這是我在處理類...爲什麼處理(Java)中的這個靜態屬性會引發錯誤?
class Stripe {
PShape s;
float x, y, w;
static Stripe lastStripe;
Stripe() {
x = width/2;
y = 0;
w = 150;
s = createShape();
s.beginShape();
s.fill(0);
s.noStroke();
s.vertex(w, 0);
s.bezierVertex(w + 50, height * 1/4, w - 50 , height * 1/3, w, height);
s.vertex(0, height);
s.bezierVertex(-50, height * 1/3, 50 , height * 1/4, 0, 0);
//s.vertex(0, 0);
//s.bezierVertex(50, 50, 50, 50, 100, 100);
s.endShape(CLOSE);
}
void draw() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
void move() {
x = x + STRIPE_VEL;
if (x > (width + OFFSCREEN_BUFFER)) {
x = Stripe.lastStripe.x - STRIPE_SPACING;
Stripe.lastStripe = this;
}
}
}
當我嘗試編譯我收到以下錯誤...
場lastStripe只能被聲明爲靜態的;靜態字段只能在靜態或頂級類型中聲明
看着這個Java tutorial,這似乎是一個有效的Java模式。雖然上面的代碼是自引用的,但如果類型更改爲int
或類似的代碼,它仍會引發相同的錯誤。
這裏有什麼問題?
編輯:通過請求,這裏是在另一個選項卡中的草圖的其餘部分。我開始認爲Processing的方法就是在類上聲明像'全局'而不是靜態變量這樣的變量......我可能會這麼做。
float STRIPE_VEL = 0.5;
float OFFSCREEN_BUFFER = 500;
float STRIPE_SPACING = 50;
int numStripes = 0;
Stripe[] stripes;
void setup() {
float offset = 0;
size(800, 600, P2D);
smooth();
numStripes = (width + 2 * OFFSCREEN_BUFFER)/STRIPE_SPACING;
stripes = new Stripe[numStripes];
for (int i=0; i < numStripes; i++) {
stripes[i] = new Stripe();
stripes[i].x = offset;
offset = offset + inc;
}
Stripe.lastStripe = stripes[0];
}
void draw() {
background(255);
for (int i=0; i < numStripes; i++) {
stripes[i].draw();
stripes[i].move();
}
//blurAll();
}
由錯誤消息,似乎這個類是一個嵌套類。 –
可能的重複[爲什麼我不能聲明和初始化內部類中的靜態變量?](http://stackoverflow.com/questions/22297722/why-cant-i-declare-and-initialize-static-variable-in -inner-class) – user2864740
這是Processing中的一個'頂級'類,即在單獨的選項卡中,但編譯時的Processing可能會將它嵌入另一個類中,我不知道。我沒有Java的經驗,但我確實知道C#非常好,是的,我不會在C#中這樣編程,但Processing是以類似C的方式實現的。 – Brendan