惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
月光博客
月光博客
T
Tailwind CSS Blog
小众软件
小众软件
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
B
Blog RSS Feed
博客园 - 司徒正美
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
博客园 - Franky
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - patrickwai

Java中哈希表(Hashtable)是如何实现的 注意for循环中变量的作用域 用SAX Parser(Simple API for XML)分析RSS android权限 【转】分治法求最大值最小值 JPA实体关系映射 JPA基本实体映射学习 Servlet实现基本文件上传 Servlet学习(一) 用javascript与正则表达式验证表单 - patrickwai - 博客园 java题目分析 java中的synchronized Spring学习——依赖注入 Spring学习——Hello World xml,javascript,XMLHttpRequest 学习笔记 向依赖关系宣战——依赖倒置、控制反转和依赖注入辨析[转载] MySql存储过程学习 自己用C语言写的扫雷算法 移位运算实际应用——判断整数N是否为2的阶次方
java enum用法
patrickwai · 2014-11-21 · via 博客园 - patrickwai
  1. 基本用法

    enum Day {
    SUNDAY, MONDAY, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY;
    }

    枚举是常量,所以应该用大写。

  2. 枚举是对象

    枚举隐含地继承了java.lang.Enum,所以它具有java.lang.Enum的属性和方法。遍历枚举:

    public class Main {
    public static void main(String[] args) {
    for(Day day:Day.values()) {
    System.out.println(day);
    }
    }
    }

  3. 枚举可以带字段和方法,以下示例来自官方The Java™ Tutorials

    public enum EnumDemo {
    AOBJECT("field one", "field two");

    private String field1;
    private String field2;

    EnumDemo(String val1, String val2){
    this.field1 = val1;
    this.field2 = val2;
    }

    public void printFields(){
    System.out.println(this.field1);
    System.out.println(this.field2);
    }

    public static void main(String[] args) {
    EnumDemo.AOBJECT.printFields();
    }
    }

    以下现实中的示例来自官方的Java Tutorial:

    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 = 120;
    for(Planet p: Planet.values()){
    System.out.println(p.surfaceGravity());
    System.out.println(p.surfaceWeight(earthWeight/EARTH.surfaceGravity()));
    }
    }
    }

  4. 枚举是singleton,可以用枚举来构建一个Singleton

    public enum Singleton {
    INSTANCE(new String[]{"arg1", "arg2"});

    String[] myArgs;
    Singleton(String[] args){
    this.myArgs = args;
    }

    public static Singleton getInstance(){
    return INSTANCE;
    }

    public static void main(String[] args) {
    for(String arg : INSTANCE.myArgs)
    System.out.println(arg);
    }
    }

posted on 2014-11-21 02:26  patrickwai  阅读(2476)  评论()    收藏  举报