我有这个代码:
public void onPlayerInteract(PlayerInteractEvent event) {
final Action action = event.getAction();
Location l1 = null;
Location l2 = null;
if (action == Action.LEFT_CLICK_BLOCK){
l1 = event.getClickedBlock().getLocation();
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l2 = event.getClickedBlock().getLocation();
}
Thread t = new Thread() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000*60*60);
Location maxx = l1.getX();
Location maxy = l1.getY();
Location maxz = l1.getZ();
Location minx = l2.getX();
Location miny = l2.getY();
Location minz = l2.getZ();
if(l1.getX() > l2.getX()){
//I can't execute this, errors!
}
} catch (InterruptedException ie) {
}
}
}
};
t.start();
它给了我错误,并说将 l1 和 l2 更改为决赛。如果我将 l1 和 l2 更改为 finals,它会给我另一个错误,它说 l1 = 等,它说删除 final。
l1
和l2
是方法onPlayerInteract()
的局部变量。在此方法中,您将创建一个匿名内部类,该类使用这些局部变量l1
和l2
。这只有在l1
和l2
是最终的时才有可能。但根据定义,最终变量只能赋值一次,然后你赋值 null,然后给它们赋另一个值。因此,您需要复制l1
并l2
最终变量,并在匿名类中使用这些最终副本:
public void onPlayerInteract(PlayerInteractEvent event) {
final Action action = event.getAction();
Location l1 = null;
Location l2 = null;
if (action == Action.LEFT_CLICK_BLOCK){
l1 = event.getClickedBlock().getLocation();
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l2 = event.getClickedBlock().getLocation();
}
final Location l1Final = l1;
final Location l2Final = l2;
Thread t = new Thread() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000*60*60);
Location maxx = l1Final.getX();
Location maxy = l1Final.getY();
Location maxz = l1Final.getZ();
Location minx = l2Final.getX();
Location miny = l2Final.getY();
Location minz = l2Final.getZ();
if(l1Final.getX() > l2Final.getX()){
// ...
}
} catch (InterruptedException ie) {
}
}
}
};
...
}
public void onPlayerInteract(PlayerInteractEvent event) {
final Action action = event.getAction();
final Location blockLocation = event.getClickedBlock().getLocation();
final Location l1 = (action == Action.LEFT_CLICK_BLOCK) ? blockLocation : null;
final Location l2 = (action == Action.RIGHT_CLICK_BLOCK) ? blockLocation : null;
Thread t = new Thread() {
...
}
}
您需要对匿名内部类使用 final。如您所知,最终引用无法修改。
JB Nizet的答案是正确的。
但请注意,不是 JB Nizet 代码:
Location l1 = null;
Location l2 = null;
if (action == Action.LEFT_CLICK_BLOCK){
l1 = event.getClickedBlock().getLocation();
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l2 = event.getClickedBlock().getLocation();
}
final Location l1Final = l1;
final Location l2Final = l2;
您可以使用以下代码
final Location l1;
final Location l2;
if (action == Action.LEFT_CLICK_BLOCK){
l1 = event.getClickedBlock().getLocation();
l2 = null;
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l1 = null;
l2 = event.getClickedBlock().getLocation();
} else {
l1 = null;
l2 = null;
}
由于局部变量永远不会初始化(甚至为 null),编译器通常会告诉您在使用之前对其进行初始化。但是,如果您在 if/elseif/else 结构的所有情况下初始化它,编译器肯定您在任何情况下都已初始化它。
无论如何,您的代码似乎没有任何意义,因为在任何情况下,l1 或 l2 都是空的。因此,您的线程将始终抛出 NullPointerException。
下面解释了为什么必须对匿名内部类使用 final:为什么我们将 final 关键字与匿名内部类一起使用?
另请注意,不能使用 if(l1.getX()> l2.getX()){因为 getX() 和 getY() 返回位置并且无法使用>运算符进行比较。您应该考虑使用 Comparable on Location 类,然后执行 if ( l1.getX().compareTo(l2.getX())> 0 ) { ... }
运行中的代码实际上是它自己的方法。你可以把它变成一个单独的可运行方法,并在实例化时将y1和y2传递给它,或者在运行中声明y1和y2。
解决方案如 @JB Nizet 所解释的那样,但如果变量具有 setter 方法,则无需复制它们。然后,您的代码将如下所示(也修复了空指针,如 @ 所述:
public void onPlayerInteract(PlayerInteractEvent event) {
final Action action = event.getAction();
final Location l1 = new Location(); // Assumming Location has a default constructor.
final Location l2 = new Location();
if (action == Action.LEFT_CLICK_BLOCK){
l1.setX(event.getClickedBlock().getLocation().getX());
l1.setY(event.getClickedBlock().getLocation().getY());
} else if (action == Action.RIGHT_CLICK_BLOCK) {
l2.setX(event.getClickedBlock().getLocation().getX());
l2.setY(event.getClickedBlock().getLocation().getY());
}
Thread t = new Thread() {
@Override
public void run() {
while(true) {
try {
Thread.sleep(1000*60*60);
Location maxx = l1Final.getX();
Location maxy = l1Final.getY();
Location maxz = l1Final.getZ();
Location minx = l2Final.getX();
Location miny = l2Final.getY();
Location minz = l2Final.getZ();
if(l1.getX() > l2.getX()){
// ...
}
} catch (InterruptedException ie) {
}
}
}
};
...
}
@Kkkev asnwer 也应该正常工作,但要注意空指针。