简体   繁体   English

java enum getter转换为int时出错

[英]Error with java enum getter converting to int

So I read a few articles here on stackoverflow and feel like I got most of it working, but they don't usually have an example on how to use the getters/setters 因此,我在这里阅读了一些有关stackoverflow的文章,并觉得其中的大部分内容都可以正常工作,但是他们通常没有关于如何使用getter / setter的示例。

I'm super close to figuring out this issue just having a hard time figuring out this last part 我非常想弄清楚这个问题,只是很难弄清楚这最后一部分

Currently all stores in a program i'm using is using openShop(id); 目前,我正在使用的程序中的所有商店都在使用openShop(id); but i'd like to be able to use openShop("GENERAL_STORE"); 但我希望能够使用openShop(“ GENERAL_STORE”); because that is much more maintainable and easy to see without having to look up the id. 因为这样更易于维护,而且无需查找ID即可轻松查看。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

private enum shopName {
    GENERAL_STORE(577);

    private final int id;
    shopName(int id) {
        this.id = id;
    }
    public int getId() {
        return this.id;
    }
};

public void openShop(String name) {
    int shopId = shopName(name).getId(); //line with error
    //int shopId = shopName.shopName(name).getId(); //I've also tried this
    openShop(shopId);
}
public void openShop(int id) {
    /* blah */
}

Here is the error i'm getting 这是我遇到的错误

$ ./compiler.sh
file.java:#: error: cannot find symbol
    int shopId = shopName(name).getId();
                 ^
  symbol:   method shopName(String)
  location: class File
1 error

shopName(name) doesn't do what you think it does. shopName(name)不会执行您认为的操作。 This code is trying to invoke method shopName and pass name as argument, but you don't have such method. 这段代码试图调用方法shopName并将name作为参数传递,但是您没有这种方法。

If you want to get shopName enum based on provided name use shopName.valueOf(name) . 如果要基于提供的名称获取shopName枚举,请使用shopName.valueOf(name) So your code should probably look like: 因此您的代码应该看起来像:

int shopId = shopName.valueOf(name).getId();

Also be aware that this method will throw IllegalArgumentException if you will not provide proper name. 另请注意,如果您不提供适当的名称,则此方法将引发IllegalArgumentException

Also enum is considered as type , so its name should start with uppercase like other Java (non-primitive) types. 枚举也被视为type ,因此它的名称应像其他Java(非原始)类型一样以大写字母开头。 So change shopName to ShopName . 因此,将shopName更改为ShopName

You say openShop("GENERAL_STORE") is more maintainable, but openShop(ShopName.GENERAL_STORE) (or the shorter openShop(GENERAL_STORE) ) would be even more maintainable, because the compiler would catch typos. 您说openShop("GENERAL_STORE")更具可维护性,但是openShop(ShopName.GENERAL_STORE) (或更短的openShop(GENERAL_STORE) )将更具可维护性,因为编译器将捕获拼写错误。

The is one of the main purposes of enums. 这是枚举的主要目的之一。

public enum ShopName {
    GENERAL_STORE(577);

    private final int id;
    private ShopName(int id) {
        this.id = id;
    }
    int getId() {
        return this.id;
    }
};

public void openShop(ShopName name) {
    openShop(name.getId());
}
public void openShop(int id) {
    /* blah */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM