创建一个带有来自父类的“final”变量的可运行对象?

huangapple go评论68阅读模式
英文:

Create Runnable with "final" variable from the parent?

问题

Create multiple new Runnable inside a loop ?

在循环内部创建多个新的Runnable?

I want to create multiple new instances of Runnable that calls another method and passing the "i" parameter from the parent's loop.

我想创建多个新的Runnable实例,这些实例调用另一个方法,并传递父循环中的"i"参数。

I do not want to use Threads since those runnables will be used as actions for keyboard shortcuts (using KeyCombination and accelerators).
(ex: Ctrl + 1, Ctrl + 2, ... Ctrl + 9)

我不想使用线程,因为这些Runnable将用作键盘快捷方式的操作(使用KeyCombination和加速器)。
(例如:Ctrl + 1、Ctrl + 2、... Ctrl + 9)

The content of the runnable that I want looks like this :

我想要的Runnable的内容如下:

if (shortcutsEnabled && checkDestinationValidity(i)) {
    copyImage(i);
}

possibly important : the number of "i" will go up to 9.

可能重要的是:"i"的数量将增加到9。

Is there any way to do this without having to make a premade table of Runnable ?

有没有办法做到这一点,而不必创建预先制作的Runnable表格?

When I try to just put the variable inside, the NetBeans IDE shows me this message :
local variables referenced from a lambda expression must be final or effectively final

当我尝试将变量放在内部时,NetBeans IDE显示了以下消息:
"从lambda表达式引用的局部变量必须是final或有效final"

I have found a solution that has delayed my "deadline" for the solution :

我找到了一个解决方案,这个解决方案推迟了我的解决方案的"截止日期":

for (int i = 0; i < copyKeyCombinations.size(); i++) {
    final int index = i;
    Runnable copyRunnable = () -> {
        if (shortcutsEnabled && checkDestinationValidity(index)) {
            copyImage(index);
        }
    };
    accelerators.put(copyKeyCombinations.get(i), copyRunnable);
}

For the record, the runnable are registered before the loop ends, and the solution above works but I was asked to find another solution

值得注意的是,在循环结束之前,这些Runnable已经被注册,上面的解决方案有效,但我被要求找到另一种解决方案

However, this is a solution that is not accepted for my work situation, I am looking for any alternatives.

然而,这个解决方案在我的工作情况下不被接受,我正在寻找任何其他替代方案。

(Note : this loop will only be launched one time, at the start of the app).

(注意:这个循环只会在应用程序启动时启动一次)。

英文:

Create multiple new Runnable inside a loop ?

I want to create multiple new instances of Runnable that calls another method and passing the "i" parameter from the parent's loop.

I do not want to use Threads since those runnables will be used as actions for keyboard shortcuts (using KeyCombination and accelerators).
(ex: Ctrl + 1, Ctrl + 2, ... Ctrl + 9)

The content of the runnable that I want looks like this :

if (shortcutsEnabled &amp;&amp; checkDestinationValidity(i)) {
    copyImage(i);
}

possibly important : the number of "i" will go up to 9.

Is there any way to do this without having to make a premade table of Runnable ?

When I try to just put the variable inside, the NetBeans IDE shows me this message :
local variables referenced from a lambda expression must be final or effectively final

I have found a solution that has delayed my "deadline" for the solution :

for (int i = 0; i &lt; copyKeyCombinations.size(); i++) {
    final int index = i;
    Runnable copyRunnable = ()-&gt; {
        if (shortcutsEnabled &amp;&amp; checkDestinationValidity(index)) {
            copyImage(index);
        }
    };
    accelerators.put(copyKeyCombinations.get(i), copyRunnable);
}

For the record, the runnable are registered before the loop ends, and the solution above works but I was asked to find another solution

However, this is a solution that is not accepted for my work situation, I am looking for any alternatives.

(Note : this loop will only be launched one time, at the start of the app).

答案1

得分: 0

您可以按照标准方式实现 `Runnable`,作为一个类而不是作为 lambda 函数
```java
public class CopyRunnable implements Runnable {

  private final int index;
  private final boolean shortcutsEnabled;

  public CopyRunnable(int index, boolean shortcutsEnabled) {
    this.index = index;
    this.shortcutsEnabled = shortcutsEnabled;
  }

  @Override
  public void run() {
    if (shortcutsEnabled && checkDestinationValidity(index)) {
      copyImage(index);
    }
  }
}
for (int i = 0; i < copyKeyCombinations.size(); i++) {
  Runnable copyRunnable = new CopyRunnable(i, shortcutsEnabled);
  //对其进行一些操作
}

<details>
<summary>英文:</summary>

You can implement `Runnable` the &quot;standard&quot; way, as a class, not as lambda function:

public class CopyRunnable implements Runnable {

private final int index;
private final boolean shortcutsEnabled;

public CopyRunnable(int index, boolean shortcutsEnabled) {
this.index = index;
this.shortcutsEnabled = shortcutsEnabled;
}

@Override
public void run() {
if (shortcutsEnabled && checkDestinationValidity(index)) {
copyImage(index);
}
}
}

for (int i = 0; i < copyKeyCombinations.size(); i++) {
Runnable copyRunnable = new CopyRunnable(i, shortcutsEnabled);
//do something with it
}


</details>



huangapple
  • 本文由 发表于 2023年5月25日 19:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定