如何自定义 enum类型

package com.jin.enums;

public enum Planet {
	MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7),
    PLUTO   (1.27e+22,  1.137e6);

    private final double mass;   // in kilograms
    private final double radius; // in meters
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    public double mass()   { return mass; }
    public double radius() { return radius; }

    // universal gravitational constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    public double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    public double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
}

 形式二

package com.jin.enums;

public enum Operation {
//	PLUS, MINUS, TIMES, DIVIDE;
//
//    // Do arithmetic op represented by this constant
//    double eval(double x, double y){
//        switch(this) {
//            case PLUS:
//            {
//            	System.out.println("aa");
//            	System.out.println("aa");
//            	System.out.println("aa");
//            	return x + y;
//            }
//            case MINUS:  return x - y;
//            case TIMES:  return x * y;
//            case DIVIDE: return x / y;
//        }
//        throw new AssertionError("Unknown op: " + this);
//    }
	
	PLUS   { double eval(double x, double y) { return x + y; } },
	MINUS  { double eval(double x, double y) { return x - y; } },
	TIMES  { double eval(double x, double y) { return x * y; } },
	DIVIDE { double eval(double x, double y) { return x / y; } };

	// Do arithmetic op represented by this constant
	abstract double eval(double x, double y);
}

 形式三:

package com.jin.enums;

public enum Color {
	RED, GREEN, YELLOW, BLUCK
}

 

 使用:

package com.jin.enums;

public class Test {
	public static void main(String[] args) {
//		System.out.println(Color.valueOf("RED"));
//		System.out.println(Planet.VENUS.radius());
		System.out.println(Operation.PLUS.eval(1, 3));
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
定义EnumTypeHandler是一种用于处理枚举类型类型处理器。在MyBatis中,通过自定义EnumTypeHandler可以实现对枚举类型的映射转换。 要自定义EnumTypeHandler,首先需要实现TypeHandler接口,并重写其相应的方法,包括setParameter、getResult和getType方法。在setParameter方法中,可以将枚举类型转换为与数据库中对应的类型,并将其设置为参数;在getResult方法中,可以将数据库中的值转换为枚举类型;而getType方法用于指定MyBatis应该对该类型处理器处理的Java类型。 例如,假设有一个枚举类型Status,包含两个值:SUCCESS和FAILURE。则可以自定义一个EnumTypeHandler<Status>来处理该枚举类型。具体实现代码如下: ``` public class StatusEnumTypeHandler extends BaseTypeHandler<Status> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Status parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i, parameter.getCode()); } @Override public Status getNullableResult(ResultSet rs, String columnName) throws SQLException { int code = rs.getInt(columnName); if (rs.wasNull()) { return null; } else { return Status.fromCode(code); } } @Override public Status getNullableResult(ResultSet rs, int columnIndex) throws SQLException { int code = rs.getInt(columnIndex); if (rs.wasNull()) { return null; } else { return Status.fromCode(code); } } @Override public Status getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { int code = cs.getInt(columnIndex); if (cs.wasNull()) { return null; } else { return Status.fromCode(code); } } @Override public Class<Status> getJavaType() { return Status.class; } } ``` 以上代码中,setNonNullParameter方法将枚举类型Status转换为int类型,并将其设置为参数;getNullableResult方法将数据库中的值转换为Status类型;getJavaType方法指定了处理的Java类型为Status。 通过自定义EnumTypeHandler,可以实现对枚举类型的映射转换,并在MyBatis的映射文件中配置使用该类型处理器来处理相应的枚举类型字段。这样,在进行数据库操作时,MyBatis会自动将枚举类型与数据库中的值进行转换,简化了对枚举类型的处理过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值