1

可能重复:
无法对非静态字段进行静态引用

我在这里创建了这段代码:http: //mibpaste.com/naGV58 使用 arraylists 来获得所需的结果。如您所见,每个代理都使用到达(PVector); 方法。

我尝试通过实现继承和多态性来重新配置整个过程,以创建一些子类等。这里的新代码:http: //mibpaste.com/WwZBWz 当我尝试应用特定方法时会产生问题。错误是:

无法对非静态字段 MW2.location 进行静态引用

你知道如何解决它吗?我想我必须从每个子类创建新对象,但我不知道如何......

我尝试过的任何事情都已经失败了。我什至不确定语法。

任何帮助将非常感激!

agents = new ArrayList<Capsule>(); // creating the agents

for (int i = 0; i < 85; i++) {
if (i < 20) agents.add(new MW1(random(width),random(height),color(255,0,0),i));
else if (i < 45) agents.add(new FW1(random(width),random(height),color(0,255,0),i));
else if (i < 65) agents.add(new MW2(random(width),random(height),color(155,0,0),i));
else agents.add(new FnW2(random(width),random(height),color(0,155,0),i));
}
}


//--------------configuring the canvas & the counter-------------


void draw() {
background(255);
time(); //reference to the visual timer


if(millis() - time >= wait){
println(counter);//if it is, do something
temp=counter;
time = millis();//also update the stored time
counter++; // counter augmentation

if(counter >180){ // check whether time exceeds 180 sec. aka 24:00 hours
counter=0; 
}
}

//-------------------------------------------------------------

work.Default(); //main-basic functions of (W) facility
retail.Default(); //main-basic functions of (R) facility
school.Default(); //main-basic functions of (S) facility
leisure.Default(); //main-basic functions of (L) facility

PVector vectorOfwork = new PVector(work.location.x, work.location.y); 
PVector vectorOfretail = new PVector(retail.location.x, work.location.y); 
PVector vectorOfschool = new PVector(school.location.x, school.location.y); 
PVector vectorOfleisure = new PVector(leisure.location.x, leisure.location.y); 

for( Capsule agent : agents) { // instancing - creating objects
agent.Default(); // global separation that affects ALL the agents
agent.separate(agents); // global separation that affects ALL the agents

if (counter>0 && counter<=30) {

if (agent instanceof FnW2)
agent.arrive(MW2.location); 
}
}
}
4

0 回答 0