如何在枚举中调用其他方法? [英] How to call additional method in enums?

查看:358
本文介绍了如何在枚举中调用其他方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

($)
{
public String getName {
returnHuge;
}

public String getContry()
{
returnIndia;
} //附加方法
},
OVERWHELMING(16)
{
public String getName()
{
returnOVERWHELMING ;
}
};
private int盎司;

public int getOunes()
{
return盎司;
}

public String getName()
{
returnPonds;
}

Enum1(int ounces1)
{
ounces = ounces1;
}
}

class EnumAsInnerClass
{
Enum1 enumInnerClass;

public static void main(String [] args)
{
EnumAsInnerClass big = new EnumAsInnerClass();
big.enumInnerClass = Enum1.BIG;
EnumAsInnerClass over = new EnumAsInnerClass();
over.enumInnerClass = Enum1.OVERWHELMING;
EnumAsInnerClass huge = new EnumAsInnerClass();
huge.enumInnerClass = Enum1.HUGE;
System.out.println(big.enumInnerClass.getName()); //池塘
System.out.println(over.enumInnerClass.getName()); // OVERWHELMING
系统。 out.println(huge.enumInnerClass.getName()); //巨大的
}
}

考虑上面的例子。如何调用getCountry方法来获取巨大的数据?



如果没有办法调用此方法,为什么Java将它视为合法?

解决方案

(正如近四年以来,我原来的答案是不正确的。)



你可以甚至调用 Enum1.HUGE.getCountry()与特定的枚举,这是非常轻微的惊喜...但即使你可以,你不能这样做一个任意的 Enum1 值,这将更有用。



解决方案是阻止它成为额外的方法 - 将 getCountry()方法添加到 Enum1 中,返回默认值或者可能引发异常。 HUGE 可以覆盖该方法。



如果您可以声明一个特定的枚举值实现了界面,但似乎没有这样的语法。


enum Enum1
{
    BIG(8), HUGE(10)
    {
        public String getName()
        {
            return "Huge";
        }

        public String getContry()
        {
            return "India";
        }//additional Method
    },
    OVERWHELMING(16)
    {
        public String getName()
        {
            return "OVERWHELMING";
        }
    };
    private int ounces;

    public int getOunes()
    {
        return ounces;
    }

    public String getName()
    {
        return "Ponds";
    }

    Enum1(int ounces1)
    {
        ounces = ounces1;
    }
}

class EnumAsInnerClass
{
    Enum1 enumInnerClass;

    public static void main(String[] args)
    {
        EnumAsInnerClass big = new EnumAsInnerClass();
        big.enumInnerClass = Enum1.BIG;
        EnumAsInnerClass over = new EnumAsInnerClass();
        over.enumInnerClass = Enum1.OVERWHELMING;
        EnumAsInnerClass huge = new EnumAsInnerClass();
        huge.enumInnerClass = Enum1.HUGE;
        System.out.println(big.enumInnerClass.getName());//Ponds        
        System.out.println(over.enumInnerClass.getName());//OVERWHELMING
        System.out.println(huge.enumInnerClass.getName());//Huge
    }
}

Consider the above example. How can I call the method getCountry for HUGE?

If there is no way to call this method, why does Java treat it as legal?

解决方案

(As noted nearly 4 years later, my original answer was incorrect.)

You can't even call Enum1.HUGE.getCountry() with the specific enum, which is very slightly surprising... but even if you could, you couldn't do so with an arbitrary Enum1 value, which would be more generally useful.

The solution is to stop it from being an additional method - add a getCountry() method into Enum1, either returning a default value or maybe throwing an exception. HUGE can then override the method.

It would be nice if you could declare that a particular enum value implemented an interface, but it seems there's no syntax for that.

这篇关于如何在枚举中调用其他方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆