public class Constant {
/*
* 以下几个变量表示英雄的状态
*/
public final static int STATUS_WALKING = 0;//走
public final static int STATUS_RUNNINGING = 1;//跑
public final static int STATUS_ATTACKING = 2;//攻击
public final static int STATUS_DEFENDING = 3;//防御
public final static int STATUS_DEAD = 4;//挂了
/*
* 以下几个变量表示英雄的等级
*/
//此处略去N行代码
}
hero.setStatus(Contant.STATUS_ATTACKING);
hero.setStatus(666);
public class PlanetWithoutEnum {
public static final int PLANET_MERCURY = 0;
public static final int PLANET_VENUS = 1;
public static final int PLANET_EARTH = 2;
public static final int PLANET_MARS = 3;
public static final int PLANET_JUPITER = 4;
public static final int PLANET_SATURN = 5;
public static final int PLANET_URANUS = 6;
public static final int PLANET_NEPTUNE = 7;
}
public enum Planet {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE;
}
public enum Planet {
MERCURY(3.302e+23, 2.439e6),
VENUS(4.869e+24, 6.052e6),
EARTH(5.975e+24,6.378e6),
MARS(6.419e+23, 3.393e6),
JUPITER(1.899e+27, 7.149e7),
SATURN(5.685e+26, 6.027e7),
URANUS(8.683e+25, 2.556e7),
NEPTUNE(1.024e+26,2.477e7);
private final double mass; // In kilograms
private final double radius; // In meters
private final double surfaceGravity; // In m / s^2
// Universal gravitational constant in m^3 / kg s^2
private static final double G = 6.67300E-11;
// Constructor
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
surfaceGravity = G * mass / (radius * radius);
}
public double mass() {
return mass;
}
public double radius() {
return radius;
}
public double surfaceGravity() {
return surfaceGravity;
}
public double surfaceWeight(double mass) {
return mass * surfaceGravity; // F = ma
}
}
//注:这里对书中的代码做了微调
public class WeightTable {
public static void main(String[] args) {
printfWeightOnAllPlanets(8d);
}
public static void printfWeightOnAllPlanets(double earthWeight) {
double mass = earthWeight / Planet.EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Weight on %s is %f%n", p, p.surfaceWeight(mass));
}
}
Weight on MERCURY is 3.023254 Weight on VENUS is 7.240408 Weight on EARTH is 8.000000 Weight on MARS is 3.036832 Weight on JUPITER is 20.237436 Weight on SATURN is 8.524113 Weight on URANUS is 7.238844 Weight on NEPTUNE is 9.090108
public enum OperationUseSwitch {
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;
}
// 如果this不属于上面四种操作符,抛出异常
throw new AssertionError("Unknown operation: " + this);
}
}
public enum Operation {
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);
}
public enum DayUseAbstractMethod {
MONDAY {
@Override
void apply() {
dealWithWeekDays();//伪代码
}
},
TUESDAY {
@Override
void apply() {
dealWithWeekDays();//伪代码
}
},
WEDNESDAY {
@Override
void apply() {
dealWithWeekDays();//伪代码
}
},
THURSDAY {
@Override
void apply() {
dealWithWeekDays();//伪代码
}
},
FRIDAY {
@Override
void apply() {
dealWithWeekDays();//伪代码
}
},
SATURDAY {
@Override
void apply() {
dealWithWeekEnds();//伪代码
}
},
SUNDAY {
@Override
void apply() {
dealWithWeekEnds();//伪代码
}
};
abstract void apply();
}
public enum Day {
MONDAY(DayType.WEEKDAY), TUESDAY(DayType.WEEKDAY), WEDNESDAY(
DayType.WEEKDAY), THURSDAY(DayType.WEEKDAY), FRIDAY(DayType.WEEKDAY), SATURDAY(
DayType.WEEKDAY), SUNDAY(DayType.WEEKDAY);
private final DayType dayType;
Day(DayType daytype) {
this.dayType = daytype;
}
void apply() {
dayType.apply();
}
private enum DayType {
WEEKDAY {
@Override
void apply() {
System.out.println("hi, weekday");
}
},
WEEKEND {
@Override
void apply() {
System.out.println("hi, weekend");
}
};
abstract void apply();
}
}
public class Text {
public enum Style {
BOLD, ITALIC, UNDERLINE, STRIKETHROUGH
}
// Any Set could be passed in, but EnumSet is clearly best
public void applyStyles(Set<Style> styles) {
// Body goes here
for(Style style : styles){
System.out.println(style);
}
}
// Sample use
public static void main(String[] args) {
Text text = new Text();
text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC));
}
}
public class Herb {
public enum Type { ANNUAL, PERENNIAL, BIENNIAL }
private final String name;
private final Type type;
Herb(String name, Type type) {
this.name = name;
this.type = type;
}
@Override public String toString() {
return name;
}
}
public static void main(String[] args) {
Herb[] garden = { new Herb("Basil", Type.ANNUAL),
new Herb("Carroway", Type.BIENNIAL),
new Herb("Dill", Type.ANNUAL),
new Herb("Lavendar", Type.PERENNIAL),
new Herb("Parsley", Type.BIENNIAL),
new Herb("Rosemary", Type.PERENNIAL) };
// Using an EnumMap to associate data with an enum - Page 162
Map<Herb.Type, Set<Herb>> herbsByType = new EnumMap<Herb.Type, Set<Herb>>(
Herb.Type.class);
for (Herb.Type t : Herb.Type.values())
herbsByType.put(t, new HashSet<Herb>());
for (Herb h : garden)
herbsByType.get(h.type).add(h);
System.out.println(herbsByType);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有