有没有比模仿 splat 运算符更简单的替代方法? [英] Is there an easier alternative to mimicking the splat operator?

查看:28
本文介绍了有没有比模仿 splat 运算符更简单的替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现它在 Ruby 中可用,但我从我在 Python 中所做的工作中认出了它;splat"运算符.长话短说,我想知道是否有一种更简单的方法来完成我目前的工作,模仿splat"运算符的作用.

I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does.

我创建了一个其他人可以调用的中心方法,因为我意识到我有几个非常相似的方法,除了一些小事情之外,它们都在做相同的事情.这是方法签名:

I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature:

private String callScript(String scriptLocation, String... extraArgs) throws Exception {

我希望至少需要一个参数(scriptLocation),然后允许任意数量的额外参数.我最终要做的是创建一个 ProcessBuilder.我的愿望是做这样的事情:

I want to require at least one argument (the scriptLocation), and then allow any number of extra arguments. What I end up doing with this is creating a ProcessBuilder. My desire is to do something like this:

ProcessBuilder pb = new ProcessBuilder("something", scriptLocation, /* extraArgs */);

当然,ProcessBuilder 构造函数只接受以下内容:

But of course, the ProcessBuilder constructor only accepts things like:

  • 列表<字符串>
  • String[]
  • 字符串...

所以我的方法显然行不通.

So my approach obviously wouldn't work.

据我所知,我目前的解决方法是:

My current workaround, which works fine as far as I know, is:

List<String> finalArgs = new ArrayList<String>();
finalArgs.add("something");
finalArgs.add(scriptLocation);
finalArgs.addAll(Arrays.asList(extraArgs));
ProcessBuilder pb = new ProcessBuilder(finalArgs);

(是的,我知道我不必使用 List 并且可以只制作一个 String[])

(yes, I understand I don't have to use List and could just make a String[])

(是的,我知道我可以遍历 extraArgs 并将它们单独添加到 finalArgs,而不必使用 addAll 和 <代码>Arrays.asList())

(and yes, I understand I could loop through extraArgs and add them to finalArgs individually, instead of having to use addAll and Arrays.asList())

(是的,我知道我可以创建一个函数,通过返回与变量参数相结合的某些参数来有效地完成我的解决方法)

(and yes, I know I can make a function that effectively accomplishes my workaround by returning certain arguments combined with variable arguments)

所以我想除了最后三个陈述之外,还有什么可以实现这一目标的吗?

So I guess outside of these last three statements, is there anything that can achieve this?

推荐答案

这取决于您对更简单的定义,但您可以使用 Builder 模式编写一个类:

It depends on your definition of simpler, but you could write a class utilizing the Builder pattern:

public class MyProcessBuilder {
    List<String> args;

    MyProcessBuilder( String text, String location ) {
        args = new ArrayList<String>();
        args.add( text );
        args.add( location );
    }

    MyProcessBuilder arg( String arg ) {
        args.add( arg );
        return this;
    }

    MyProcessBuilder args( String... args ) {
        this.args.addAll( Arrays.asList( args ) );
        return this;
    }

    public ProcessBuilder go() {
        return new ProcessBuilder( args );
    }

    public static MyProcessBuilder callScript( String location ) {
        return new MyProcessBuilder( "something", location );
    }

    public static void main( String[] args ) {
        callScript( "/tmp/bla" ).arg( "something else" ).arg( "more" ).go();
    }
}

这篇关于有没有比模仿 splat 运算符更简单的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆