effective java——30使用enum

Posted 高桥凉介星光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了effective java——30使用enum相关的知识,希望对你有一定的参考价值。

1,

枚举太阳系八大行星
package com.enum30.www;

public enum Planet {//枚举太阳系八大行星
     MERCURY(3.302e+23,2.439e6),
     VENUS(4.8e+24,6.0e6),
     EARTH(5.9e+24,6.3e6);

     private final double mass;    //kilogram
     private final double radius;  //meters
     private final double surfaceGravity; //In m/s^2
     private static final double G=6.67E-11;

    //consrtuctor
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
        surfaceGravity=G*mass/(radius*radius);
    }
    public double surfaceWeight(double mass){
        return mass*surfaceGravity; //F=ma
    }

    public double getMass() {
        return mass;
    }

    public double getRadius() {
        return radius;
    }

    public double getSurfaceGravity() {
        return 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/Planet.EARTH.getSurfaceGravity();
        for(Planet p:Planet.values()){
            System.out.printf("Weight on %s is %f%n", p, p.surfaceWeight(mass));
        }
    }
}

2.使用更为简洁的方式表达+ - * /

技术分享图片
package com.enum30demo2.www;

import com.sun.org.apache.xpath.internal.operations.Minus;

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;
        }},
        DEVIDE{double apply(double x,double y){
            return x/y;
        }};
        abstract double apply(double x,double y);
}
View Code

3.使用更为安全的方式来执行工资计算,增大可读性,降低出错率

package com;

public enum PayrollDay {
    MONDAY(PayType.WEEKDAY),TUESDAY(PayType.WEEKDAY),
    WEDNESDAY(PayType.WEEKDAY),THURSDAY(PayType.WEEKDAY),
    FRIDAY(PayType.WEEKDAY),SATURDAY(PayType.WEEKEND),
    SUNDAY(PayType.WEEKEND);
        private  PayType payType;


     PayrollDay(PayType payType) {
        this.payType=payType;
    }
    double pay(double homeWork,double payRate){
        return payType.pay(homeWork,payRate);
    }

    private enum PayType{
            WEEKDAY{
                double overtimePay(double hours,double payRate){
                    return hours<=HOURS_PER_SHIFT? 0:(hours-HOURS_PER_SHIFT)*payRate;
                }
            },
            WEEKEND{
                double overtimePay(double hours,double payRate){
                    return hours*payRate;
                }
            };
             private  final static int HOURS_PER_SHIFT=8;
            abstract double overtimePay(double hrs,double payRate);
            double pay(double homeWork,double payRate){
                double basePay=(homeWork-HOURS_PER_SHIFT)*payRate;
                return  basePay+overtimePay(homeWork,payRate);
            }

        }

    public static void main(String[] args) {
        double hours=8.8;
        double payRate=20.0;
        double total=MONDAY.pay(hours,payRate);
        System.out.println(total);
     }
}

 

以上是关于effective java——30使用enum的主要内容,如果未能解决你的问题,请参考以下文章

Java:Effective java学习笔记之 用enum代替int常量

《Effective Java》第6章 枚举和注解

Effective C++ 条款2:尽量以const,enum,inline替换#define

Effective C++条款 02:尽量以 const, enum, inline 替换 #define

Effective Java 读书笔记之五 枚举和注解

Java Enum 上的 OpenAPI 和 @Schema 注释