当前位置:编程学习 > html/css >>

Enum的使用

使用枚举可以限定使用值的范围(离散变量)
 
首先定义可以取的值:
 
[html]  
package 枚举;  
  
public enum Day {  
    MONDAY, TUESDAY, WEDNESDAY,  
    THURSDAY, FRIDAY, SATURDAY,SUNDAY   
}  
 对该值的操作
 
[html]  
package 枚举;  
  
public class EnumTest {  
    Day day;  
      
    public EnumTest(Day day) {  
        this.day = day;  
    }  
      
    public void tellItLikeItIs() {  
        switch (day) {  
            case MONDAY:  
                System.out.println("Mondays are bad.");  
                break;  
                      
            case FRIDAY:  
                System.out.println("Fridays are better.");  
                break;  
                           
            case SATURDAY:   
            case SUNDAY:  
                System.out.println("Weekends are best.");  
                break;  
                          
            default:  
                System.out.println("Midweek days are so-so.");  
                break;  
        }  
    }  
      
    public static void main(String[] args) {  
        System.out.println("Day.MONDAY="+Day.MONDAY);  
        System.out.println("Day.MONDAY.name()="+Day.MONDAY.name());  
        System.out.println("Day.MONDAY.ordinal()="+Day.MONDAY.ordinal());  
        EnumTest firstDay = new EnumTest(Day.MONDAY);  
        firstDay.tellItLikeItIs();  
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);  
        thirdDay.tellItLikeItIs();  
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);  
        fifthDay.tellItLikeItIs();  
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);  
        sixthDay.tellItLikeItIs();  
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);  
        seventhDay.tellItLikeItIs();  
    }  
}  
输出结果:
 
Day.MONDAY=MONDAY
Day.MONDAY.name()=MONDAY
Day.MONDAY.ordinal()=0
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
[html]  
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 su易做图ceGravity() {  
        return G * mass / (radius * radius);  
    }  
    double su易做图ceWeight(double otherMass) {  
        return otherMass * su易做图ceGravity();  
    }  
    public static void main(String[] args) {  
        if (args.length != 1) {  
            System.err.println("Usage: java Planet <earth_weight>");  
            System.exit(-1);  
        }  
        double earthWeight = Double.parseDouble(args[0]);  
        double mass = earthWeight/EARTH.su易做图ceGravity();  
        for (Planet p : Planet.values())  
           System.out.printf("Your weight on %s is %f%n",  
                             p, p.su易做图ceWeight(mass));  
    }  
}  
枚举类型变量可以直接调用方法
 
在命令行输入参数 175
 
输出:
 
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
补充:web前端 , HTML/CSS  ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,