[英]Multiple consecutive calls of same method without repeating the method name
Is it possible to call a method multiple times in a row and pass in different arguments each time, but without repeating the method name when calling?是否可以连续多次调用一个方法,每次传入不同的arguments,但调用时不重复方法名?
For example, in JavaFX, you can add nodes to another node by calling the getChildren.add()
method of the parent node each time:例如在JavaFX中,可以通过每次调用父节点的
getChildren.add()
方法将节点添加到另一个节点:
GridPane gridPane = new GridPane();
Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();
gridpane.getChildren.add(button);
gridpane.getChildren.add(label);
gridpane.getChildren.add(checkbox);
As you can see, it can get quite repetitive.如您所见,它可能会变得非常重复。 Is it possible to make the calls without repeating the method name?
是否可以在不重复方法名称的情况下进行调用? I would like to know if I could do something like:
我想知道我是否可以做类似的事情:
GridPane gridPane = new GridPane();
Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();
gridPane.getChildren.add {
button;
label;
checkbox;
}
Please note that I am not suggesting the syntax above to be added into the Java language.请注意,我并不是建议将上述语法添加到 Java 语言中。 It was just written to help clarify my point.
它只是为了帮助澄清我的观点而写的。
I don't think it is possible but you can put all the desired string in an array then do a for loop:我认为这是不可能的,但您可以将所有所需的字符串放在一个数组中,然后执行一个 for 循环:
String[] arr ={"foo","bar","foobar"};
for(int i=0;i<3:i++){
m(arr[i]);
}
There's no syntax similar to your example.没有类似于您的示例的语法。 The closest you can get for the same effect is either with loops (like in zazz's answer ) or with the Stream API.
获得相同效果的最接近方法是使用循环(如 zazz的回答)或使用 Stream API。
Depending on whether you care about the return value or not, something like取决于您是否关心返回值,例如
List.of("foo", "bar", "foobar").forEach(x -> m(x));
or或者
Stream.of("foo", "bar", "foobar").map(x -> m(x)).collect(Collectors.toList());
void m(String value) {
System.out.println(value);
}
void m(String[] values) {
for (String value : values) {
m(value);
}
}
Usage:用法:
m("foo") // foo
m("bar") // bar
m({"foo", "bar"});
// foo
// bar
void m(String... values) {
for (String value in values) {
System.out.println(value);
}
}
Usage:用法:
m("foo") // foo
m("bar") // bar
m("foo", "bar");
// foo
// bar
That's not supported in Java's syntax. Java 的语法不支持这一点。 The philosophy of Java mainly is to express your program quite explicitly.
Java 的哲学主要是非常明确地表达你的程序。 So, if you want three method calls, you have to write down three method calls (or introduce some kind of loop or use streams).
所以,如果你想要三个方法调用,你必须写下三个方法调用(或者引入某种循环或使用流)。
To explain why such a syntax isn't part of Java, I'll discuss your idea as a potential language extension to Java where something like为了解释为什么这样的语法不是 Java 的一部分,我将讨论你的想法作为 Java 的潜在语言扩展,其中类似
m {{
"foo";
"bar";
"foobar";
}};
has the same meaning as具有相同的含义
m("foo");
m("bar");
m("foobar");
If asked about such a multi-call feature, I'd definitely vote against its introduction into Java.如果被问及这样的多呼叫功能,我肯定会投票反对将其引入 Java。 Why:
为什么:
m{{...}}
multi-call expression return all the values as an array?m{{...}}
多调用表达式是否将所有值作为数组返回? As a List? I guess that the people responsible for Java's future will at least share some of my doubts, so I bet such a feature will never make it into Java.我想负责 Java 未来的人至少会分享我的一些疑问,所以我敢打赌这样的功能永远不会进入 Java。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.