"嵌套类型无法隐藏封闭类型" class - Java Processing

时间:2015-11-24 19:47:45

标签: java processing

我收到此错误:The nested type cannot hide an enclosing type。我查了一下,其他人似乎两次宣布他们的班级,我不是。

奇怪的是,如果我将代码复制并粘贴到新的处理文档中,它就可以正常工作。但是当我将它转换为js时,我需要在保存并再次打开后才能正常工作。

MotorBike Bike1, Bike2, Bike3, Bike4, Bike5, Bike6, Bike7, Bike8, Bike9;
int Score_Bike1 = 0;
int Score_Bike2 = 0;
int Score_Bike3 = 0;
int Score_Bike4 = 0;
int Score_Bike5 = 0;
int Score_Bike6 = 0;
int Score_Bike7 = 0;
int Score_Bike8 = 0;
int Score_Bike9 = 0;
String Score_Spacing = "        ";
int GameState = 0;


class MotorBike {
float Pos_X;
int Pos_Y;
float Speed;
int Size = 30;
float WheelSize = Size / 3;
color Color;


MotorBike(int Declare_X, int Declare_Y, int Declare_Speed, color Declare_Color)                                                                                                    
{
this.Pos_X = Declare_X;
this.Pos_Y = Declare_Y;
Speed = Declare_Speed;
Color = Declare_Color;
}

void move()
{
if (GameState == 1) {
  Speed = (random(0, 50) / 10);
  Pos_X = Pos_X + Speed;
}
}

void render()
{
fill(Color);
triangle(Pos_X, Pos_Y, Pos_X + Size, Pos_Y, Pos_X + Size / 2, Pos_Y -Size /      2);
fill(255);
strokeWeight(1.5);
ellipse(Pos_X, Pos_Y, WheelSize, WheelSize);
ellipse(Pos_X + Size, Pos_Y, WheelSize, WheelSize);
}
}

void setup()
{
size(700, 600);
background(200);
SpawnBikes();
}

void draw()
{
background(200);
strokeWeight(3);
line(50, 10, 50, 590);
line(650, 10, 650, 590);
strokeWeight(1);
MoveBikes(); 
DetectWinner();
DisplayScore();
}

void MoveBikes()
{
Bike1.render();
Bike1.move();
Bike2.render();
Bike2.move();
Bike3.render();
Bike3.move();
Bike4.render();
Bike4.move();
Bike5.render();
Bike5.move();
Bike6.render();
Bike6.move();
Bike7.render();
Bike7.move();
Bike8.render();
Bike8.move();
Bike9.render();
Bike9.move();
}

void DetectWinner()
{
textSize(15);
fill(0); 
if (Bike1.Pos_X >= 620) {
noLoop(); 
text("Bike 1 Wins", 310, 10, 350, 50); 
Score_Bike1 += 1;
GameState = 2;
}
if (Bike2.Pos_X >= 620) {
noLoop(); 
text("Bike 2 Wins", 310, 10, 350, 50); 
Score_Bike2 += 1;
GameState = 2;
}
if (Bike3.Pos_X >= 620) {
noLoop(); 
text("Bike 3 Wins", 310, 10, 350, 50); 
Score_Bike3 += 1;
GameState = 2;
}
if (Bike4.Pos_X >= 620) {
noLoop(); 
text("Bike 4 Wins", 310, 10, 350, 50); 
Score_Bike4 += 1;
GameState = 2;
}
if (Bike5.Pos_X >= 620) {
noLoop(); 
text("Bike 5 Wins", 310, 10, 350, 50); 
Score_Bike5 += 1;
GameState = 2;
}
if (Bike6.Pos_X >= 620) {
noLoop(); 
text("Bike 6 Wins", 310, 10, 350, 50); 
Score_Bike6 += 1;
GameState = 2;
}
if (Bike7.Pos_X >= 620) {
noLoop(); 
text("Bike 7 Wins", 310, 10, 350, 50); 
Score_Bike7 += 1;
GameState = 2;
}
if (Bike8.Pos_X >= 620) {
noLoop(); 
text("Bike 8 Wins", 310, 10, 350, 50); 
Score_Bike8 += 1;
GameState = 2;
}
if (Bike9.Pos_X >= 620) {
noLoop(); 
text("Bike 9 Wins", 310, 10, 350, 50); 
Score_Bike9 += 1;
GameState = 2;
}
}

void DisplayScore()
{
textSize(15);
fill(0); 
text("Bike 1: " + Score_Bike1 + Score_Spacing + "Bike 2: " + Score_Bike2 +   Score_Spacing + "Bike 3: " + 
Score_Bike3 + Score_Spacing +  "Bike 4: " + Score_Bike4 + Score_Spacing +   "Bike 5: " + Score_Bike5 + Score_Spacing + 
"Bike 6: " + Score_Bike6 + Score_Spacing +  "Bike 7: " + Score_Bike7 + Score_Spacing +  "Bike 8: " + Score_Bike8 + 
Score_Spacing + "Bike 9: " + Score_Bike9, 65, 530, 635, 700);
}

void keyPressed()
{
if (keyPressed) { 
if (key == ' ') 
{
  if (GameState == 0) {
    GameState = 1;
  }
  if (GameState == 2) {
    loop();
    background(200);
    SpawnBikes();
    GameState = 0;
  }
  }
}
}

void SpawnBikes()
{
Bike1 = new MotorBike(50, 100, 2, color(255, 0, 0));
Bike2 = new MotorBike(50, 150, 2, color(0, 255, 0)); 
Bike3 = new MotorBike(50, 200, 2, color(0, 0, 255)); 
Bike4 = new MotorBike(50, 250, 2, color(255, 255, 0));
Bike5 = new MotorBike(50, 300, 2, color(0, 255, 255)); 
Bike6 = new MotorBike(50, 350, 2, color(255, 0, 255)); 
Bike7 = new MotorBike(50, 400, 2, color(100, 255, 0));
Bike8 = new MotorBike(50, 450, 2, color(0, 100, 255)); 
Bike9 = new MotorBike(50, 500, 2, color(255, 0, 100));
}

1 个答案:

答案 0 :(得分:2)

您的问题是由于您将草图命名为与草图中使用的类相同的事实引起的。如果您的草图中有MotorBike类,则无法将草图命名为MotorBike

重命名草图,或重命名您的课程。

在幕后,这是因为Processing将草图导出为Java类,草图中的任何类都成为该Java类的内部类。所以你的草图就是这样的:

class MotorBike{
   void draw(){
      //whatever
   }

   class MotorBike{
      int x;
      //whatever
   }
}

这是非法的Java,这是造成错误的原因。您不能拥有与父类同名的内部类。换句话说,嵌套类型不能隐藏封闭类型。

这也是为什么当你将它复制到一个新的草图时它可以正常工作 - 处理为你的草图提供了一个随机的默认名称,所以在你将草图保存为其他东西之前你没有这个名字碰撞。