网站地图    收藏   

主页 > 前端 > css教程 >

Enum的使用 - html/css语言栏目:html.css - 自学php

来源:自学PHP网    时间:2015-04-14 14:51 作者: 阅读:

[导读] 使用枚举可以限定使用值的范围(离散变量)首先定义可以取的值:[html] package 枚举; public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,SUNDAY } 对该值的操作...

使用枚举可以限定使用值的范围(离散变量)
 
首先定义可以取的值:
 
[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 surfaceGravity() {  
        return G * mass / (radius * radius);  
    }  
    double surfaceWeight(double otherMass) {  
        return otherMass * surfaceGravity();  
    }  
    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.surfaceGravity();  
        for (Planet p : Planet.values())  
           System.out.printf("Your weight on %s is %f%n",  
                             p, p.surfaceWeight(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

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论