Can I add a function to enums in Java?

前端 未结 4 857
长情又很酷
长情又很酷 2020-12-14 05:21

I have an enum, which looks like

public enum Animal {
  ELEPHANT,
  GIRAFFE,
  TURTLE,
  SNAKE,
  FROG
}

and I want to do something like

4条回答
  •  悲&欢浪女
    2020-12-14 06:14

    As well as using the techniques above which add a field to the enumerated type you can also use a pure method based approach and polymorphism. This is more "OOP style" but I would not say it is necessarily better.

    Unfortunately, you may (see the comment below) need to define an interface:

    public interface AnimalTraits {
        default boolean isAmphibian()   { return false; };
        default boolean isReptile()     { return false; };
        default boolean isMammal()      { return false; };
    }
    

    But then you can them implement the interface in each of your enumeration elements:

    public enum Animal implements AnimalTraits {
    
         ELEPHANT   { @Override public boolean isMammal()    { return true; } },
         GIRAFFE    { @Override public boolean isMammal()    { return true; } },
         TURTLE     { @Override public boolean isReptile()   { return true; } },
         SNAKE      { @Override public boolean isReptile()   { return true; } },
         FROG       { @Override public boolean isAmphibian() { return true; } }
    }
    

    Note that I use default implementations in the interface to cut down on the amount of typing you need in the enum.

    Regarding the necessity of the interface: I tried adding the methods in the interface as abstract methods at the top of the enum and Eclipse seemed to allow it and insisted on implementations in the enum elements, but then failed to compile those properly. So it looks like it ought to be possible without an interface, but perhaps it is not yet implemented in the compiler.

提交回复
热议问题