简单整数枚举

发布于 2024-11-25 09:29:10 字数 276 浏览 1 评论 0原文

我是枚举的新手,我想创建一个枚举来将整数与更容易理解的定义进行比较。

if (getFruit() == myEnum.APPLE) {
    // ...
}

而不是

if (getFruit() == 1) {
    // ...
}

Where getFruit() 返回诸如12 等值

I'm new with enums and I'd like to create one to compare an integer to a more comprehensible definition.

if (getFruit() == myEnum.APPLE) {
    // ...
}

instead of

if (getFruit() == 1) {
    // ...
}

Where getFruit() returns values like 1,2 and so on

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

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

发布评论

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

评论(4

我还不会笑 2024-12-02 09:29:10

您错过了枚举的要点...您使用它们而不是“老派”int常量。

看看这个:

public enum Fruit {
    APPLE,
    ORANGE,
    BANANA
}

public class Dessert {

    private Fruit fruit;

    public Dessert(Fruit fruit) {
        this.fruit = fruit;
    }

    public Fruit getFruit() {
        return fruit;
    }
}

public class Test {

    Dessert dessert = new Dessert(Fruit.APPLE);
    if (dessert.getFruit() == Fruit.ORANGE) {
        // nope
    } else if (dessert.getFruit() == Fruit.APPLE) {
        // yep
    }
}

You are missing the point of enums... you use them instead of "old school" int constants.

Have a look at this:

public enum Fruit {
    APPLE,
    ORANGE,
    BANANA
}

public class Dessert {

    private Fruit fruit;

    public Dessert(Fruit fruit) {
        this.fruit = fruit;
    }

    public Fruit getFruit() {
        return fruit;
    }
}

public class Test {

    Dessert dessert = new Dessert(Fruit.APPLE);
    if (dessert.getFruit() == Fruit.ORANGE) {
        // nope
    } else if (dessert.getFruit() == Fruit.APPLE) {
        // yep
    }
}
染墨丶若流云 2024-12-02 09:29:10

您可以使用 getFruit() == myEnum.APPLE.ordinal() ,其中 ordinal 是您在文件中声明枚举的顺序。

public enum myEnum {
     APPLE, ORANGE, BANANA;
}

在这种情况下,APPLE 的序数是 0,ORANGE 是 1,BANANA 是 2。

或者您可以执行以下操作:

public enum myEnum {
    APPLE(1), ORANGE(2), BANANA(3);

    private final int i;

    private myEnum(int i){
        this.i=i;
    }

    public int getNumber(){
        return i;
    }
}

但是,我只是让 getFruit() 返回一个枚举。

You can use getFruit() == myEnum.APPLE.ordinal() where ordinal is the order you declare the enums in your file.

public enum myEnum {
     APPLE, ORANGE, BANANA;
}

The ordinal for APPLE in this case is 0, ORANGE is 1, BANANA is 2.

Or you could do the following:

public enum myEnum {
    APPLE(1), ORANGE(2), BANANA(3);

    private final int i;

    private myEnum(int i){
        this.i=i;
    }

    public int getNumber(){
        return i;
    }
}

However, I would just make getFruit() return an enum.

一腔孤↑勇 2024-12-02 09:29:10

您可以创建这样的枚举

public enum myEnum {
    APPLE  (1),
    ORANGE (2);
}

您还可以查看此 教程

You can create an enum like this

public enum myEnum {
    APPLE  (1),
    ORANGE (2);
}

You can also check this tutorial

剩余の解释 2024-12-02 09:29:10
public enum Fruit {
    APPLE  ("apple"), ORANGE ("orange");

    private String value;

    private Fruit(int v) { value = v; }
}

getFruit 必须返回 Fruit enum

public Fruit getFruit() {
    return aFruit;
}

现在你可以使用

if (getFruit() == Fruit.APPLE) { //and so on
    // ...
}

如果你使用 enum,最好使用 switch-case

switch(getFruit()) {
    case Fruit.APPLE: ... break;
    case Fruit.ORANGE: ... break;
}
public enum Fruit {
    APPLE  ("apple"), ORANGE ("orange");

    private String value;

    private Fruit(int v) { value = v; }
}

getFruit must return Fruit enum

public Fruit getFruit() {
    return aFruit;
}

Now you can use

if (getFruit() == Fruit.APPLE) { //and so on
    // ...
}

And if you use enum, its better to use switch-case

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