zoukankan      html  css  js  c++  java
  • Enum的使用

    使用枚举可以限定使用值的范围(离散变量)

    首先定义可以取的值:

    package 枚举;
    
    public enum Day {
        MONDAY, TUESDAY, WEDNESDAY,
        THURSDAY, FRIDAY, SATURDAY,SUNDAY 
    }
    

     对该值的操作

    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.

    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

    出自:http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html



     

  • 相关阅读:
    UVA 12657 Boxes in a Line 双向链表模拟
    C语言单片和C#语言服务器端DES及3DES加密的实现
    关于TcpClient,Socket连接超时的几种处理方法
    拿来参考的学习计划
    faire la course
    今日法语2
    炸鱼
    今日法语
    今日疑问
    下周想做的菜
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986825.html
Copyright © 2011-2022 走看看