将枚举作为方法中的参数传递?
我有一个抽象类 Medium,其中一个数据成员是枚举。
private Taal talenOndertiteling[];
public enum Taal {
NEDERLANDS, FRANS, DUITS, ENGELS, SPAANS, ITALIAANS
}
public Taal[] getTalenOndertiteling() {
return talenOndertiteling;
}
public void setTalenOndertiteling(Taal[] talenOndertiteling) {
this.talenOndertiteling = talenOndertiteling;
}
现在,当我尝试像这样调用最后一个方法时:
BD bd1 = new BD();
bd1.setTalenOndertiteling(Taal.ENGELS);
我收到错误。 (BD 类实现 Medium 类)关于我应该如何调用该方法有什么想法吗?如果我想设置几种语言,我该怎么做?
谢谢!
I have an abstract class Medium, where one of the datamembers is an enum.
private Taal talenOndertiteling[];
public enum Taal {
NEDERLANDS, FRANS, DUITS, ENGELS, SPAANS, ITALIAANS
}
public Taal[] getTalenOndertiteling() {
return talenOndertiteling;
}
public void setTalenOndertiteling(Taal[] talenOndertiteling) {
this.talenOndertiteling = talenOndertiteling;
}
Now when I try to call the last method like this:
BD bd1 = new BD();
bd1.setTalenOndertiteling(Taal.ENGELS);
I'm getting an error. (The BD class implements the Medium class) Any ideas on how I should be calling the method? And what if I wanted to set several languates, how would I do that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的方法接受
Taal[]
,但您的调用传递Taal
。您有两种选择:要么传递显式的
Taal[]
:或者更改方法声明以采用 vararg 参数并让编译器为您执行此操作:
Your method accepts a
Taal[]
but your invocation passes aTaal
. You have two choices:Either pass an explicit
Taal[]
:or, change the method declaration to take a vararg parameter and have the compiler do it for you:
您必须创建一个包含一个元素的数组并将其传递给该方法:
You have to create an array with one element and pass it to the method:
setter 需要一个 Taal 枚举数组。
所以正确的用法是:
或更详细:
The setter expects an array of Taal enums.
So the proper use is:
or more verbose:
setTalenOrdertiteling
获取一个数组。所以正确的调用应该是:
The
setTalenOrdertiteling
gets an array.So the correct call should be:
两个问题:
1)setTalenOndertiteling()需要Taal数组,所以
2)Taal似乎是一个嵌套枚举(在BD中?),没有特定的导入,你需要做
two issues:
1) setTalenOndertiteling() is expecting array of Taal, so
2) Taal seems to be a nested enum (in BD?), without specific import, you need to do