繁体   English   中英

枚举如何在枚举对象中返回枚举的字符串内容

[英]Enum how to return enum's string content inside enum object

public enum STUFF
{
    THING("Ok"), STUFF("Sweet"), PEOPLE("umm"), CAR("Vrrm");

    String contents;

    STUFF(String x)
    {
       contents = x;
    }

    public String getContents()
    {
        return ??
    }

}

所需结果:

System.out.print(STUFF.CAR.getContents());
//Vrrm

您应该在以下位置查看行星示例: http : //docs.oracle.com/javase/tutorial/java/javaOO/enum.html

public enum STUFF
{
    THING("Ok"), STUFF("Sweet"), PEOPLE("umm"), CAR("Vrrm");

    private final String contents;

    STUFF(String x)
    {
        contents = x;
    }

    public String getContents()
    {
        return contents;
    }
 }

构造函数中的分配是错误的。

x = contents;

应该: -

contents = x;

并且getContents()的返回值应为:-

public String getContents()
{
    return contents;
}

暂无
暂无

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

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