枚舉傳參,枚舉使用詳解


作者:NiceCui

  • 本文謝絕轉載,如需轉載需征得作者本人同意,謝謝。
  • 本文鏈接:http://www.cnblogs.com/NiceCui/p/7835122.html
  • 郵箱:moyi@moyibolg.com
  • 日期:2017-11-14 22:26

將枚舉作為參數傳遞在復雜的服務調用中也是很常見的,大型互聯網公司中都會寫很多對本身服務中實體內容的描述和擴展,使用枚舉去查詢或者去展示都會顯得邏輯很清楚,而且能夠重復利用,修改非常方便,枚舉中嵌套枚舉

在我們的數據庫設計中很多時候都會把一些類型什么的使用數字符號代替,當類型數量不是很多,或者復雜度沒有那么高的情況下,都會在創建相關聯的枚舉。

這樣的枚舉怎么使用呢,當我們的使用velocity引擎模板或者其他的前端展示層模板的時候,我們會把查出的實體對象傳遞到視圖層,

這時候如果有數字或者字母代替某個類型的字段我們如果使用循環比較的話就會看起來相當的麻煩,這個時候我們可以將對應的枚舉對象傳遞過去這個時候我們只需要把對應字段放到

枚舉中就可以了,就會得到我們得到代替符號背后我們需要的真實名字。

簡單例子:

1、先來一個簡單的枚舉類

 1 package com.blog.NiceCui.Enum;
 2 
 3 /**
 4  * 
 5  * @author NiceCui
 6  * @date 2017-11-14
 7  * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 8  *
 9  */    
10 public enum BusinessEnum {
11            
12         Order(2,"訂單業務"),
13         User(3,"用戶業務");
14         
15         private int type;
16         private String name;
17         
18         private BusinessEnum(int type,String name){
19             this.type=type;
20             this.name=name;
21         }
22         public int getType() {
23             return type;
24         }
25         public void setType(int type) {
26             this.type = type;
27         }
28         public String getName() {
29             return name;
30         }
31         public void setName(String name) {
32             this.name = name;
33         }
34         
35         public static BusinessEnum getEnumByType(int type){
36             for(BusinessEnum bt:values()){
37                 if(bt.type==type){
38                     return bt;
39                 }
40             }
41             return null;
42         }
43         
44     }

 

2、寫一個簡單的實體對象

 1 package com.blog.NiceCui.entity;
 2 
 3 
 4 /**
 5  * 
 6  * @author NiceCui
 7  * @date 2017-11-14
 8  * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 9  *
10  */
11 public class Business {
12     
13     //id
14     private int id;
15     //業務類型id
16     private long typeId;
17     //用戶id
18     private long userId;
19     //用戶姓名
20     private String userName;
21     
22     
23     public Business() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27     public Business(int id, long typeId, long userId, String userName) {
28         super();
29         this.id = id;
30         this.typeId = typeId;
31         this.userId = userId;
32         this.userName = userName;
33     }
34     public int getId() {
35         return id;
36     }
37     public void setId(int id) {
38         this.id = id;
39     }
40     public long getTypeId() {
41         return typeId;
42     }
43     public void setTypeId(long typeId) {
44         this.typeId = typeId;
45     }
46     public long getUserId() {
47         return userId;
48     }
49     public void setUserId(long userId) {
50         this.userId = userId;
51     }
52     public String getUserName() {
53         return userName;
54     }
55     public void setUserName(String userName) {
56         this.userName = userName;
57     }
58     @Override
59     public String toString() {
60         return "Business [id=" + id + ", typeId=" + typeId + ", userId=" + userId + ", userName=" + userName + "]";
61     }
62     
63     
64 
65 }

 3、寫一個簡單的接口和實現只做簡單的模擬

package com.blog.NiceCui.service;

import com.blog.NiceCui.Enum.BusinessEnum;
import com.blog.NiceCui.entity.Business;

/**
 * 
 * @author NiceCui
 * @date 2017-11-14
 * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 *
 */
public interface BusinessService {
    
    
    /**
     * @describe 這里只是一個簡單的模擬一下服務
     * @param id
     * @return
     */
    
    /*通過對應的枚舉得到對應的業務名字*/
    public String getByEnum(BusinessEnum bt,Business business);
    
    /*通過id得到對應的實體對象*/
    public Business getById(long id);
}

package com.blog.NiceCui.service;

import com.blog.NiceCui.Enum.BusinessEnum;
import com.blog.NiceCui.entity.Business;

/**
 * 
 * @author NiceCui
 * @date 2017-11-14
 * @cnblog 地址:http://www.cnblogs.com/NiceCui/
 *
 */
public class BusinessServiceImpl implements BusinessService {

    public Business getById(long id) {
        // TODO Auto-generated method stub
        return null;
    }

    public String getByEnum(BusinessEnum bt) {
        // TODO Auto-generated method stub
        return bt.getName();
    }

}

 

4、輸出

 1 public static void main(String[] args) {
 2         
 3         //int id, long typeId, long userId, String userName
 4         Business business = new Business(1,2L,3L,"hello enum");
 5         
 6         BusinessService businessService = new BusinessServiceImpl();
 7         
 8         
 9         String aa = businessService.getByEnum(BusinessEnum.getEnumByType(business.getTypeId()));
10         
11         System.out.println("對應的業務是:"+aa);
12     }
    

5、如果我們前端頁面調用:

<div class="form-group col-xs-12 col-sm-6 col-md-4">
   $!{BusinessEnum.
getEnumByType(business.getTypeId())}: $!{business.userName}
</div>

 6、枚舉使用交互框當做參數傳遞,需要枚舉實體.class:

1 beat.getModel().add("CardTypeEnum",CardTypeEnum.class);//證件類型等級枚舉

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM