你的位置:首页 > 软件开发 > Java > 枚举传参,枚举使用详解

枚举传参,枚举使用详解

发布时间:2017-11-15 23:00:30
作者:NiceCui本文谢绝转载,如需转载需征得作者本人同意,谢谢。本文链接 22:26将枚举作为参数传递在复杂的服务调用中也是很常见的,大型互联网公司中都会写很多对本身服务中实体内容的描述和扩展,使用枚举去查询或者去展示都会显得逻辑很清楚,而且能够重复利用,修改非常方便,枚举中 ...

枚举传参,枚举使用详解

作者:NiceCui

  • 本文谢绝转载,如需转载需征得作者本人同意,谢谢。
  • 本文链接 22:26

将枚举作为参数传递在复杂的服务调用中也是很常见的,大型互联网公司中都会写很多对本身服务中实体内容的描述和扩展,使用枚举去查询或者去展示都会显得逻辑很清楚,而且能够重复利用,修改非常方便,枚举中嵌套枚举

在我们的数据库设计中很多时候都会把一些类型什么的使用数字符号代替,当类型数量不是很多,或者复杂度没有那么高的情况下,都会在创建相关联的枚举。

这样的枚举怎么使用呢,当我们的使用velocity引擎模板或者其他的前端展示层模板的时候,我们会把查出的实体对象传递到视图层,

这时候如果有数字或者字母代替某个类型的字段我们如果使用循环比较的话就会看起来相当的麻烦,这个时候我们可以将对应的枚举对象传递过去这个时候我们只需要把对应字段放到

枚举中就可以了,就会得到我们得到代替符号背后我们需要的真实名字。

简单例子:

1、先来一个简单的枚举类

 1 package com.blog.NiceCui.Enum; 2  3 /** 4  *  5  * @author NiceCui 6  * @date 2017-11-14 7  * @cnblog 地址: 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 地址: 9  *10 */11 public class Business {12  13  //id14  private int id;15  //业务类型id16  private long typeId;17  //用户id18  private long userId;19  //用户姓名20  private String userName;21  22  23  public Business() {24   super();25   // TODO Auto-generated constructor stub26  }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  @Override59  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 地址: * */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 地址: * */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);//证件类型等级枚举

 

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:枚举传参,枚举使用详解

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录