java 自定义枚举

public enum Planet {
	MERCURY(3.3,2.4),
	VENUS(4.8,6.052);
	
	private final double mass;
	private final double radius;
	private final double surface;
	private static final double G=6.6;
	
	Planet(double mass,double radius) {
		this.mass=mass;
		this.radius=radius;
		surface=G*mass/(radius*radius);
	}
	public double mass(){
		return mass;
	}
	public double radius(){
		return radius;
	}
	public double surfaceGravity(){
		return surface;
	}
	
	public double surfaceWeight(double mass){
		return mass* surface;
	}
public enum Operation {
	PLUS,MINUS,TIMES,DIVIDE;
	double apply(double x, double y){
		switch (this) {
		case PLUS:	return  x+y;
		case MINUS: return x-y;
		case TIMES :return x*y;
		case DIVIDE:return x/y;
	}
		throw new AssertionError("Unknow op:"+this);
	}
}
enum Operation2 {
	PLUS{double apply(double x,double y){return x+y;}},
	MINUS{double apply(double x,double y){return x-y;}},
	TIMES{double apply(double x,double y){return x*y;}},
	DIVIDE{double apply(double x,double y){return x/y;}};
	abstract double apply(double x, double y);
}
enum Operation3 {
	PLUS("+"){
		double apply(double x,double y){return x+y;}
	},
	MINUS("-"){
		double apply(double x,double y){return x-y;}
	},
	TIMES("*"){
		double apply(double x,double y){return x*y;}
	},
	DIVIDE("/"){
		double apply(double x,double y){return x/y;}
	};
	private final String symbol;
	Operation3(String symbol) {
		this.symbol=symbol;
	}
	
	@Override
	public String toString() {
		return symbol;
	}
	abstract double apply(double x, double y);
}


你可能感兴趣的:(java,枚举)