先说下enum这个类
private enum Color
{
red(1), blue(3), green(7);
static int value;
Color(int a)
{
value=a;
}
public static int getValue()
{
return value;
}
}
注意
1、enum就相当于一个类!所以最后没有 分号!!(所以可以在内部,或在外部)
2、枚举值 必须放在类的开头!!
3、如果声明变量 如这里的value也只能在 枚举的值的后面!!
4、可以有diy自己的构造函数!但枚举值必须按照构造函数进行 初始化!!
例如这里 如果写个 black 那他会提示错误1!
下面给出一个nb写的
综合运用
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);
private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius)
{
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
double surfaceGravity()
{
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args)
{
double earthWeight = Double.parseDouble("175");
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}
细心的朋友 注意到没???
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
格式化输出!!
http://lz12366.iteye.com/blog/678613