Java – use enumeration values instead of enumeration class names

I use static enumeration in the interface and want to use it in the extension class

I have the following interfaces:

public interface StateSupport {
    public static enum State {
        NEW,UNCHANGED,UPDATED;
    }
}

and

public interface Support extends StateSupport  {
    public void do(Context arg0);
}

Last shift

public class MyClassUtil implements Support {
    public void do(Context arg0){ 
        MyClass obj = new MyClass(NEW);
    }

}

The key is that I don't want to write "state. New", just "new": -)

So how can I use enumeration names? Is there a way?

Solution

You can use static import:

import static com.yourpackage.StateSupport.State.NEW;
import static com.yourpackage.StateSupport.State.UNCHANGED;
import static com.yourpackage.StateSupport.State.UPDATED;

Or short (discouraged):

import static com.yourpackage.StateSupport.State.*;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>