java枚舉類型變通

來源:互聯網
上載者:User

標籤:c中   return   封裝對象   com   開發   ring   png   沒有   用法   

原始用法

public enum Color {    RED, GREEN, BLANK, YELLOW  }  

開發中用法 

public enum ApiCodeEnum {    SUCCESS(0,"ok"),    USER_NOT_EXISTS(1,"使用者不存在!"),    CODE_ERROR(2,"驗證碼不正確!"),    BUY_FAIL(3,"購買失敗!"),    NOT_LOGGED(4,"沒有登入!"),    NO_AUTH(5,"沒有許可權!"),    //欄位    private Integer errno;    private String msg;    //建構函式    ApiCodeEnum(Integer errno,String msg) {        this.errno = errno;        this.msg = msg;    }    //擷取枚舉中的資料    public Integer getErrno() {        return errno;    }    //擷取枚舉中的資料    public String getMsg() {        return msg;    }}//返回成json格式封裝對象public class ApiCodeDto {    //返回碼    private Integer errno;    //返回內容    private String msg;    public ApiCodeDto() {}    //手動建立返回資訊代碼及資訊內容    public ApiCodeDto(Integer errno, String msg) {        this.errno = errno;        this.msg = msg;    }    //根據枚舉內容封裝資訊代碼及資訊內容    public ApiCodeDto(ApiCodeEnum apiCodeEnum) {        this.errno = apiCodeEnum.getErrno();        this.msg = apiCodeEnum.getMsg();    }}/*Spring MVC中返回實體物件會自動轉換成json格式*/return new ApiCodeDto(ApiCodeEnum.LOGIN_SUCCESS);

前端返回結果:

 

java枚舉類型變通

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.