函数的参数

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

Arguments of a function

问题

我需要一个具有更多参数的函数,如下所示:

private GroupLayout function(Container host, Class J1, String nameofJ1, Class J2, String nameofJ2,..................Class Jn, String nameofJn) {
    //函数体
}

一个用于在多个应用程序中使用的可定制的Grouplayout函数。

我尝试过:

private GroupLayout function(Container host, Class ...arg, String ...arg1) {
    //函数体
}

但是没有起作用,它说“可变参数必须是最后一个参数,并且使用class会有一些问题。

例如,我想要一个函数调用,如:

function(NamePanel, JButton, "xxx", JMenu, "yyy", JComboBox, "zzz")

并在NamePanel上创建一个名为“xxx”的JButton,名为“yyy”的JMenu,依此类推。

有人知道如何解决这个问题吗?

英文:

I need a function with more arguments as:

private GroupLayout function(Container host, Class J1, String nameofJ1, Class J2, String nameofJ2,..................Class Jn, String nameofJn) {
//body
}

a fuction for a customizable Grouplayout for use in more applications.

I tried:

private GroupLayout function(Container host, Class ...arg, String ...arg1) {
    //body
}
but didn't work, it said "varargs parameter must be the last parameter, and with class are some problems.

for example, I'd like a function call as:
function(NamePanel, JButton, "xxx", JMenu, "yyy", JComboBox, "zzz") and to former a GroupLayout on NamePanel with JButton named "xxx", JMenu named "yyy" and so one.

Is somebody who knows a solve of this problema?

答案1

得分: 2

Varargs

你不能多次使用可变参数(varargs)。为什么?因为编译器会不知道你传入了什么参数。

看一个仅使用 String 的例子,编译器会把第一个参数识别为 String,而其余的参数(0 到 n)会被视为可变参数(编译成数组):

static void function(String arg, String ...arg1) { }                    // 编译通过
// 这是完全有效的,第一个是 'arg',其余的是 'arg1'
function("1", "2", "3");  

但是当有多个可变参数时,就不工作了,因为编译器无法确定 'arg1' 何时结束、'arg2' 何时开始:

static void function(String arg, String ...arg1, String ...arg2) { }    // 无法编译通过
// '2' 和 '3' 到底是 'arg1' 还是 'arg2'?是空的还是不同的?
function("1", "2", "3");  

因此,可变参数必须始终是形式参数中的最后一个

解决方法

你似乎想要传递一个 Class 和一个 String 的组合。传递这些组合的最佳方式是使用基于键值的结构 Map<Class, String>,这也保证了键和值的数量相等。

private GroupLayout function(Container host, Map<Class<?>, String> classStringMap) {
    // 方法体
}
Map<Class<?>, String> map = new HashMap<>();
map.put(J1.class, "J1 的名称");
map.put(J2.class, "J2 的名称");
...
function(container, map);
英文:

Varargs

You cannot use varargs more than once. Why? The compiler will be clueless what parameters you pass in.

See the example using String only where the compiler the first parameter is a String and the rest (0..n) is vararg (compiled into an array):

static void function(String arg, String ...arg1) { }                    // COMPILES
// this is perfectly valid, the first one is &#39;arg&#39; and the rest is &#39;arg1&#39;
function(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;);  

This one with multiple varargs doesn't work, as long as the compiler doesn't know where arg1 ends and arg2 starts:

static void function(String arg, String ...arg1, String ...arg2) { }    // DOESN&#39;T COMPILE
// are both &quot;2&quot; and &quot;3&quot; an &#39;arg1&#39; and &#39;arg2&#39; is empty or different?
function(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;);  

For this reason, varargs must be always the last formal parameter.

Workaround

You seem want to pass a pair of Class and String. The best way to pass these pairs is to use a key-value based structure Map&lt;Class, String&gt; which also guarantees the same number of keys and values is equal.

private GroupLayout function(Container host, Map&lt;Class&lt;?&gt;, String&gt; classStringMap) {
    // method body
}
Map&lt;Class&lt;?&gt;, String&gt; map = new HashMap&lt;&gt;();
map.put(J1.class, &quot;name of J1&quot;);
map.put(J2.class, &quot;name of J2&quot;);
...
function(container, map);

答案2

得分: -1

Builder模式 是你的朋友。

这是Java Builder实现

还要看看Lombok的Builder注解

英文:

Builder pattern is your friend.

This is java builder implementation.

Look at Lombok Builder annotation as well.

huangapple
  • 本文由 发表于 2020年8月21日 01:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510539.html
匿名

发表评论

匿名网友

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

确定