zoukankan      html  css  js  c++  java
  • java通过接口扩展枚举

    package com.hra.riskprice;
    
    import com.hra.riskprice.SysEnum.Factor_Type;
    import com.hra.riskprice.pojo.SysEnumDict;
    import com.sun.corba.se.spi.orb.Operation;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import java.util.*;
    
    @SpringBootApplication
    public class RiskpriceApplication {
         interface  Operation{
        double apply(double x,double y);
    
    }
        enum  BasicOperation implements Operation {
    
            PLUS("+"){
                public double apply(double x, double y) {
                    return x+y;
                }
            },
            MINUS("-"){
    
                public double apply(double x, double y) {
                    return x-y;
                }
            },
            TIMS("*"){
    
                public double apply(double x, double y) {
                    return x*y;
                }
            },
            DIVIDE("/"){
    
                public double apply(double x, double y) {
                    return x/y;
                }
            };
            private  final  String symbol;
            BasicOperation(String symbol){
                this.symbol=symbol;
    
            }
    
            @Override
            public String toString() {
                return symbol;
            }
        }
        public static void main(String[] args) {
            BasicOperation b;
            b=BasicOperation.PLUS;
            Integer[] ids= Integer.class.getEnumConstants();
           test(BasicOperation.class,1,3);//主要方法 下面是获取枚举集合的列举方法
            EnumSet<BasicOperation> ops= EnumSet.allOf(BasicOperation.class);//在java中为枚举专门设计 效率高 第一名
            BasicOperation[] op2s= BasicOperation.values();//效率没有EnumSet高 第二名
            BasicOperation[] op1s= BasicOperation.class.getEnumConstants(); //如果不是枚举返回空 任何类型都点的出来 注意用到了.class  第三名
        }
        private static <T extends  Enum<T> & Operation> void test(Class<T> opSet,double x,double y){
    
            for(Operation op:opSet.getEnumConstants()){
               System.out.println(x+" "+op +" "+ y+" ="+op.apply(x,y));
    
            }
        }
    }
  • 相关阅读:
    高并发学习之使用RateLimiter实现令牌桶限流
    Tomcat学习之连接数设置
    SpringCloud学习之Zuul
    SpringCloud学习之Ribbon负载均衡
    SpringCloud学习之注册中心Eureka
    Spring源码学习之Spring IOC 容器源码分析(上)
    测试爬虫
    HTTP协议之chunk编码(分块传输编码
    转载: Qt 学习之路 2归档
    Python与机器人技术
  • 原文地址:https://www.cnblogs.com/kexb/p/10126021.html
Copyright © 2011-2022 走看看