How to call Enum individual methods?

后端 未结 2 1622
再見小時候
再見小時候 2021-01-20 02:50

I have an enum that might look like the one below. My goal is to have an enum with some common methods (I enforced this by adding an abstract method) and some \

2条回答
  •  臣服心动
    2021-01-20 03:19

    Because SPECIAL_VALUE is an instance of enum and enum has only method syso().you are calling

    Here is the same thing with classes:

    interface Foo {
    
            Foo inst1 = new Foo() {
                @Override
                public void doFoo() {
                }
    
                public void doAnonymous() {
    
                }
            };
            void doFoo();
        }
    

    You cannot call method doAnonymous() like Foo.inst1.doAnonymous() and you are able to access the doAnonymous only via reflection

提交回复
热议问题