简体   繁体   English

多次连续调用同一方法而不重复方法名

[英]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());
  1. Use overload:使用重载:
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
  1. Use varargs:使用可变参数:
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:为什么:

  • It doesn't introduce something new, something that wasn't possible before (it's just another way of writing down the same thing as before).它不会引入一些新的东西,一些以前不可能的东西(它只是写下与以前相同的东西的另一种方式)。
  • It isn't significantly shorter than the classical way.它并不比经典方式短得多。
  • Behind some (IMHO non-intuitive) braces syntax, it hides the fact that there are three method calls.在一些(恕我直言不直观的)大括号语法背后,它隐藏了存在三个方法调用的事实。 So it reduces one of the most important aspects of software quality: readability.因此它降低了软件质量最重要的方面之一:可读性。
  • Using the multi-call pattern is less flexible than the classical individual calls.使用多呼叫模式不如经典的单独呼叫灵活。 You can't later insert intermediate statements between the calls unless you change back to the classical style.除非您改回经典样式,否则以后不能在调用之间插入中间语句。
  • What do you do about the three return values of the method calls?方法调用的三个返回值怎么办? The multi-call language extension must define an answer for this question.多呼叫语言扩展必须定义此问题的答案。 Does this m{{...}} multi-call expression return all the values as an array?这个m{{...}}多调用表达式是否将所有值作为数组返回? As a List?作为列表? Only the last call's return value?只有最后一次调用的返回值? None of them?他们都没有? If you use the classical style, it's easy for developers to choose the treatment of the return values.如果使用古典风格,开发人员很容易选择返回值的处理方式。 With multi-call, they are bound to only one possible version, the one that became part of the language definition.对于多重调用,它们只能绑定到一个可能的版本,即成为语言定义一部分的版本。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM