我希望c为随机颜色,因此我可以产生许多不同颜色的敌人,但不确定如何做。如果我把Color.cyan放在里面的话,效果很好。但是,如果我想在super()中放入一个变量,它就不会那样。我是Java新手,所以我只是想弄清楚。是否可以在不将变量设为静态的情况下进行?
package first.Game;
import java.awt.Color;
import java.util.Random;
public class MenuEnemy extends Enemy{
private Random r = new Random();
private Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
public MenuEnemy(float x, float y, Handler handler)
{
super(x, y, 16, 16, 2, 8, c, ID.Enemy, handler);
}
}
问题是字段尚未初始化,因为在super之后执行了子类非静态字段初始化。
这里有几个选项供您选择,有两种可能:
c
。public static Color getRandomColor() {
Random r = new Random();
return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
}
c
来做某件事一样,因为我们将其设置为null,将其设置为null随机的静态颜色也可能会产生有害的副作用。 super(..., null, ...);
Random r = new Random();
this.c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
如果使用第一个建议,这将是最终结果。
public class Enemy {
protected float x, y;
protected int i1, i2, i3, i4;
protected Color c;
protected String id;
protected Handler handler;
public Enemy(float x, float y, int i1, int i2, int i3, int i4, Color c, String id, Handler handler) {
this.x = x;
this.y = y;
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
this.i4 = i4;
this.c = c;
this.handler = handler;
}
public static Color getRandomColor() {
Random r = new Random(); // TODO possibly reuse a static random somewhere, depends on use-case
return new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
}
}
public class MenuEnemy extends Enemy {
public MenuEnemy(float x, float y, Handler handler) {
super(x, y, 16, 16, 2, 8, Enemy.getRandomColor(), ID.Enemy, handler);
}
}
package first.Game;
import java.awt.Color;
import java.util.Random;
class ID {
public static final String Enemy = "e";
}
class Handler {
}
class Enemy {
public Enemy(float x, float y, int p1, int p2, int p3, int p4, Color c, String id, Handler handler) {
}
}
public class MenuEnemy extends Enemy {
private Random r = new Random();
private Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
protected MenuEnemy(Builder builder) {
super(builder.x, builder.y, 16, 16, 2, 8, builder.c, ID.Enemy, builder.handler);
}
public static class Builder {
private float x;
private float y;
private Handler handler;
private Random r = new Random();
private Color c;
public Builder x(float x) {this.x = x; return this;};
public Builder y(float y) {this.y = y; return this;};
public Builder handler(Handler handler) {this.handler = handler; return this;};
public Builder r(Random r) {this.r = r; return this;};
public Builder c(Color c) {this.c = c; return this;};
public MenuEnemy build() {
if(c == null)
c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
return new MenuEnemy(this);
}
}
public static void main(String[] args) {
MenuEnemy enemy = new Builder()
.x(10)
.y(10)
.handler(new Handler())
.r(new Random())
.build();
System.out.println(enemy);
}
}
可以有另一个构造函数。
package first.Game;
import java.awt.Color;
import java.util.Random;
public class MenuEnemy extends Enemy {
private Random r = new Random();
protected Color c;
public MenuEnemy(float x, float y, Handler handler)
{
this(x, y, handler, new Color((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)));
}
private MenuEnemy(float x, float y, Handler handler, Color c)
{
super(x, y, 16, 16, 2, 8, c, ID.Enemy, handler);
this.c = c;
}
}