从 EnumSet 转换设置当A继承B时

发布于 2024-11-17 16:58:05 字数 579 浏览 4 评论 0原文

标题基本上已经解释了这个问题。我有一个接口方法:

Set<Field> getFieldSet()

并且我有一个类 User ,它看起来像这样

class User {
    enum Fields implements Field {
        USERNAME, PASSWORD;
        ...
    }
    ...
}

现在我想实现 UsergetFieldSet()方法。天真的方法似乎只是 return EnumSet.allOf(Fields.class) 但我收到以下错误:

> Type mismatch: cannot convert from Set<User.Fields> to Set<Field>

除了手动将 EnumSet 复制到 Set 之外,还有其他方法吗?有好的方法吗?

The title pretty much explains the question. I have an interface method:

Set<Field> getFieldSet()

and I have a class, User which looks something like this

class User {
    enum Fields implements Field {
        USERNAME, PASSWORD;
        ...
    }
    ...
}

Now I want to implement User's getFieldSet() method. The naive way seems to just return EnumSet.allOf(Fields.class) but I get the following error:

> Type mismatch: cannot convert from Set<User.Fields> to Set<Field>

Other than manually copying the EnumSet to Set<Field>, is there a good way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

血之狂魔 2024-11-24 16:58:05

您可以返回new HashSet(EnumSet.allOf(Fields.class));

这将绕过这样一个事实:您无法将 Set 类型的值分配给 Set 类型的变量。

或者,您的界面可以是 Set getFields() 代替。您可以Set分配给捕获变量。

You could return new HashSet<Field>(EnumSet.allOf(Fields.class));.

That will get around the fact that you can't assign a value of type Set<User.Fields> to a variable of type Set<Field>.

Alternatively, your interface could be Set<? extends Field> getFields() instead. You can assign Set<User.Field> to a capturing variable.

人心善变 2024-11-24 16:58:05

使用 集合。 unmodifierSet

return Collections.<Field>unmodifiableSet(EnumSet.allOf(Fields.class));

优点:

  • 没有不安全的转换:操作在运行时是类型安全的
  • 返回的集合实际上是一个Set,而不是一个设置
  • 该集合不会被复制,只会被包装
  • 返回的集合不能被改变

缺点:

  • 返回的集合不能被改变,但无论如何这样做都是不安全的。

Use Collections.unmodifiableSet:

return Collections.<Field>unmodifiableSet(EnumSet.allOf(Fields.class));

Pros:

  • No unsafe conversion cast: the operation is typesafe at runtime
  • The returned set is actually a Set<Field>, not a Set<? extends Field>
  • The set isn't copied, only wrapped
  • The returned set cannot be mutated

Cons:

  • The returned set cannot be mutated, but it wouldn't be safe to do so anyway.
凡尘雨 2024-11-24 16:58:05

这不起作用的原因是 Set 不是 Set 的子类型。例如,如果您从方法返回 Set,您可能会遇到如下情况:

Set<Field> fieldSet = user.getFieldSet(); //Returns an EnumSet<Fields>
fieldSet.add(new Field(){}); //Would compile, but would blow up at runtime, 
                             //because the set can only contain Fields enum 
                             //constants

这里最好的选择是使用不同的 set 实现(通常是不可修改的 set)来返回价值观。例如:

Set<Field> getFieldSet() {
    return Collections.unmodifiableSet(EnumSet.allOf(Fields.class));
}

或者,如果您需要该集合是可变的(通常可能不是一个好主意)

Set<Field> getFieldSet() {
    return new HashSet(EnumSet.allOf(Fields.class));
}

The reason this doesn't work is that Set<Fields> isn't a subtype of Set<Field>. For example, if you returned a Set<Fields> from your method, you could get a situation like the following:

Set<Field> fieldSet = user.getFieldSet(); //Returns an EnumSet<Fields>
fieldSet.add(new Field(){}); //Would compile, but would blow up at runtime, 
                             //because the set can only contain Fields enum 
                             //constants

Your best option here is to use a different set implementation (usually an unmodifiable set) to return the values. For example:

Set<Field> getFieldSet() {
    return Collections.unmodifiableSet(EnumSet.allOf(Fields.class));
}

or, if you need the set to be mutable (probably not a good idea, usually)

Set<Field> getFieldSet() {
    return new HashSet(EnumSet.allOf(Fields.class));
}
旧故 2024-11-24 16:58:05

这是我认为一个很好的解决方案。这不完全是你想要的,但足够接近

import java.util.EnumSet;
import java.util.Set;

public class User {
    enum Fields implements Field {
        USERNAME,
        PASSWORD;
    }

    Set< ? extends Field> getFieldSet() {
        return EnumSet.allOf(Fields.class);
    }
}

Here is I think a good solution. It's not exactly what you want but close enough

import java.util.EnumSet;
import java.util.Set;

public class User {
    enum Fields implements Field {
        USERNAME,
        PASSWORD;
    }

    Set< ? extends Field> getFieldSet() {
        return EnumSet.allOf(Fields.class);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文