In Java, how to iterate on the constants of an interface?

前端 未结 6 1245
心在旅途
心在旅途 2021-02-07 12:51

in an interface, I store constants in this way (I\'d like to know what you think of this practice). This is just a dummy example.

interface HttpConstants {
    /         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 13:21

    public enum HttpConstant {
        /** 2XX: generally "OK" */
        HTTP_OK(200).
        HTTP_CREATED(201),
        HTTP_ACCEPTED(202),
        HTTP_NOT_AUTHORITATIVE(203),
        HTTP_NO_CONTENT(204),
        HTTP_RESET(205),
        HTTP_PARTIAL(206);
    
        private int code;
        private HttpConstant(int code) {
            this.code = code;
        }
    
        public int getCode() {
            return code;
        }
    }
    

    with HttpConstant.values().

提交回复
热议问题