将枚举作为方法中的参数传递?

发布于 2024-09-10 15:10:53 字数 581 浏览 2 评论 0原文

我有一个抽象类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌酱 2024-09-17 15:10:53

您的方法接受 Taal[],但您的调用传递 Taal。您有两种选择:

要么传递显式的 Taal[]

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});

或者更改方法声明以采用 vararg 参数并让编译器为您执行此操作:

public void setTalenOndertiteling(Taal... talenOndertiteling) {

Your method accepts a Taal[] but your invocation passes a Taal. You have two choices:

Either pass an explicit Taal[]:

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});

or, change the method declaration to take a vararg parameter and have the compiler do it for you:

public void setTalenOndertiteling(Taal... talenOndertiteling) {
稍尽春風 2024-09-17 15:10:53

您必须创建一个包含一个元素的数组并将其传递给该方法:

bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});

You have to create an array with one element and pass it to the method:

bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});
灼痛 2024-09-17 15:10:53

setter 需要一个 Taal 枚举数组。

所以正确的用法是:

BD bd1 = new BD();
bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});

或更详细:

BD bd1 = new BD();

Taal[] taals = new Taal[1];
taals[0] = Taal.ENGELS;

bd1.setTalenOndertiteling(taals);

The setter expects an array of Taal enums.

So the proper use is:

BD bd1 = new BD();
bd1.setTalenOndertiteling(new Taal[]{Taal.ENGELS});

or more verbose:

BD bd1 = new BD();

Taal[] taals = new Taal[1];
taals[0] = Taal.ENGELS;

bd1.setTalenOndertiteling(taals);
傲影 2024-09-17 15:10:53

setTalenOrdertiteling 获取一个数组。
所以正确的调用应该是:

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});

The setTalenOrdertiteling gets an array.
So the correct call should be:

bd1.setTalenOndertiteling(new Taal[] {Taal.ENGELS});
等风也等你 2024-09-17 15:10:53

两个问题:
1)setTalenOndertiteling()需要Taal数组,所以

bd.setTalenOndertiteling(new Taal[]{Taal.ENGELS};

2)Taal似乎是一个嵌套枚举(在BD中?),没有特定的导入,你需要做

bd.setTalenOndertiteling(new BD.Taal[]{BD.Taal.ENGELS};

two issues:
1) setTalenOndertiteling() is expecting array of Taal, so

bd.setTalenOndertiteling(new Taal[]{Taal.ENGELS};

2) Taal seems to be a nested enum (in BD?), without specific import, you need to do

bd.setTalenOndertiteling(new BD.Taal[]{BD.Taal.ENGELS};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文