枚举类型代替if /else

public enum TestEum {


    FAST {
        @Override
//业务逻辑
        void run(ConsumerCompanyMapper consumerCompanyMapper) {
            ConsumerCompanyExample example  = new ConsumerCompanyExample();
            example.createCriteria().andCompanyIdEqualTo(2);
            System.out.println("wo shi fast");
            System.out.println(JSONObject.toJSON(consumerCompanyMapper.selectByExample(example)));
        }
    },

    NORMAL{
//业务逻辑
        @Override
        void run(ConsumerCompanyMapper consumerCompanyMapper) {
            System.out.println("wo shi normal");
        }
    };

    abstract void run(ConsumerCompanyMapper consumerCompanyMapper);
}
public class TestEumMain {

    public  void testEum(ConsumerCompanyMapper consumerCompanyMapper) {

       //执行FAST里面的逻辑
       //例如 if(param.equals("FAST")) 执行以下逻辑
        TestEum testEum = TestEum.valueOf("FAST");
        testEum.run(consumerCompanyMapper);
    }
}

 

你可能感兴趣的:(java,java基础篇)