有没有更好/更简洁的语法?

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

Is there a better/shorter syntax for this?

问题

  1. 我想将它缩短,使它们都在一行变为真,我不知道这是否可能。

bckBtn.setEnabled(true);
cBtn.setEnabled(true);
addBtn.setEnabled(true);
btn7.setEnabled(true);

  1. ***有人给我这行代码,但我看不懂它是什么意思***

Streams.of(obj1, obj2).forEach(obj -> obj.setEnable(true));

  1. 抱歉如果问题很简单,我是 Java 新手。
  2. 再次感谢。
英文:

I want to make it shorter, making them all true in one line, I don't know if this is possible

  1. bckBtn.setEnabled(true);
  2. cBtn.setEnabled(true);
  3. addBtn.setEnabled(true);
  4. btn7.setEnabled(true);

someone gave me this line but I couldn't understand it

Streams.of(obj1, obj2).forEach(obj -> obj.setEnable(true));

sorry if the question was easy, I'm new to java.
thanks again.

答案1

得分: 2

尝试这样做:

  1. Button[] buttons = {bckBtn, cBtn, addBtn, ...};
  2. for (int i = 0; i < buttons.length; i++)
  3. {
  4. Button temp = buttons[i]; // 对 buttons[i] 对象的第二个引用
  5. temp.setEnabled(true);
  6. }
英文:

Try this:

  1. Button[] buttons = {bckBtn, cBtn, addBtn, ...};
  2. for (int i = 0; i &lt; buttons.length; i++)
  3. {
  4. Button temp = buttons[i]; // seconds reference to the buttons[i] object
  5. temp.setEnabled(true);
  6. }

答案2

得分: 2

以下是翻译好的内容:

这是我能想到的最简单、在 Java 8 之前且在概念上最容易的代码。

  1. for (Button button : Arrays.asList(bckBtn, cBtn, addBtn, etc)) {
  2. button.setEnabled(true);
  3. }
英文:

Here’s the simplest, pre-java 8 and conceptually easiest, code I could come up with.

  1. for (Button button : Arrays.asList(bckBtn, cBtn, addBtn, etc)) {
  2. button.setEnabled(true);
  3. }

huangapple
  • 本文由 发表于 2020年10月6日 02:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64214473.html
匿名

发表评论

匿名网友

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

确定