一、定义枚举类,并随机取值
第一种方法:
一、定义枚举类
首选编写枚举类型值
定义变量
构造方法
编写枚举类的内部静态方法
public enum TestEstateTypeEnum { PAYMENT_BUSSINESS("PAYMENT_BUSSINESS","商业按揭购房"), PAYMENT_FUND("PAYMENT_FUND","公积金按揭购房"), PAYMENT_BUS_FUND("PAYMENT_BUS_FUND","商业&公积金组合按揭购房"), PAYMENT_NONE("PAYMENT_NONE","无按揭购房"), MORTGAGE_OWN("MORTGAGE_OWN","持证抵押房"), SELF_HELPING("SELF_HELPING","自建房"), ; public String text; public String testStr; TestEstateTypeEnum(String text, String testStr) { this.text = text; this.testStr = testStr; } public static TestEstateTypeEnum randomType(TestEstateTypeEnum[] values){ return values[(int)(Math.random()*values.length)]; } }
二、编写测试类,随机取值
public class TestEnum { @Test public void test(){ TestEstateTypeEnum randomType = TestEstateTypeEnum.randomType(TestEstateTypeEnum.values()); System.out.println(randomType+" "+randomType.testStr);//PAYMENT_NONE 无按揭购房 } }
第二种方法:
一、自定义方法
public class SalesParameters { /** * 获取制定类型的随机枚举值 * @return String */ public static String SALES_ENUM_VALUE(String type) { String val = ""; String[] origins = {"VISIT", "RECOMMEND", "LEAFLET", "ACTIVITY", "MEDIA", "MESSAGE", "NET", "PHONESALE", "FRIENDS", "OTHERS"}; String[] selectAddresses = {"HOME_ADDRESS", "HOUSE_ADDRESS", "COMPANY_ADDRESS", "ESTATE_ADDRESS", "OTHER_ADDRESS"}; if (type.equalsIgnoreCase("origin")) { val = origins[(int) (Math.random() * origins.length)]; }else if (type.equalsIgnoreCase("selectAddress")) { val = selectAddresses[(int) (Math.random() * selectAddresses.length)]; return val; }
二、测试类调用
SALES_ENUM_VALUE("origins")
当有些人一出生就有的东西,我们要为之奋斗几十年才拥有。但有一样东西,你一辈子都不会有,那就是我们曾经一无所有。