参考Java的官方tutorialDoc整理如下。

What is Enum

An enum type is a special data type.

It enables for a variable to be a set of predefined constants. Because they are constants, the names of an enum type's fields are in UPPERCASE letters.

For example, you can specify a days-of-the-week enum type as:

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

You can use enum by . (dot), example Day.MONDAY

Enum can be more powerful. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields.

For example, you can specify a planet enum type:

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));
}
}

Enum Types的更多相关文章

  1. Effective Java 77 For instance control, prefer enum types to readResolve

    The readResolve feature allows you to substitute another instance for the one created by readObject ...

  2. Java中Enum类型的序列化(转)

    在Java中,对Enum类型的序列化与其他对象类型的序列化有所不同,今天就来看看到底有什么不同.下面先来看下在Java中,我们定义的Enum在被编译之后是长成什么样子的. Java代码: Java代码 ...

  3. Effective Java 50 Avoid strings where other types are more appropriate

    Principle Strings are poor substitutes for other value types. Such as int, float or BigInteger. Stri ...

  4. Javac语法糖之Enum类

    枚举类在Javac中是被当作类来看待的. An enum type is implicitly final unless it contains at least one enum constant ...

  5. [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)

    如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html   谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...

  6. Entity Framework 教程——模型浏览器

    模型浏览器: 在之前的章节中,我们创建了第一个关于学校的实体数据模型.但是EDM设计器并没有将他所创建的所有对象完全显示出来.它只将数据库中的被选择的表与视图显示出来了. 模型浏览器可以将EDM所创建 ...

  7. 模型浏览器【Model Browser】【EF基础系列6】

    We have created our first Entity Data Model for School database in the previous section. The visual ...

  8. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  9. BookRent借阅管理

    最近整了个BookRent的小应用,单机版.连本地sqlite db.wpf界面,其中涉及到一些有趣的小功能和小坑,简单小结一下. 项目结构是wpf ui->view model->rep ...

随机推荐

  1. python Aspscheduler 定时任务框架使用

    前几日,爬虫基本能爬点东西出来了,现在需要实现定时把数据爬到DB里去,可以使用windows定时任务执行py脚本,但好像不彻底,要做一个纯(jiao)粹(qing)的程序员,定时任务的重任落到了Asp ...

  2. Create a simple REST web service with Python--转载

    今日尝试用python建立一个restful服务. 原文地址:http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-r ...

  3. Sublime Text2 按shift键选择不了的问题

    记录下来,免得以后忘了: 今天在Sublime Text 2装了一个ThinkPHP插件之后.发现按shift键+鼠标左键选择不了内容了.原因是ThinkPHP里的热键与系统的有冲突了,须要设置例如以 ...

  4. 一个封装HTTP请求的函数(C++)

    这里封装了HTTP请求的,支持GET与POST,并支持各种参数组合,调用方式很简单使用DEVWEB::WebRequest(string(“http://www.luaie.com/”),ret);就 ...

  5. C++沉思录之二——虚函数使用的时机

    虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你 ...

  6. mybatis知识总结

    基于昨天的mybatis入门详解,今天我们再来看看mybatis稍微高深些的知识点. 1.解决Model属性和数据库字段不一致的问题 1),开启驼峰命名 2),使用resultMap进行映射, < ...

  7. 读取xml文件信息

    static void Main(string[] args) { XmlTextReader reader = new XmlTextReader(@"D:\zy\visual studi ...

  8. Layout( 布局)

    一. 加载方式//class 加载方式<div id="box" class="easyui-layout"style="width:600px ...

  9. django: template using & debug

    模板的作用方法有如下三种: blog/views.py: from django.template import loader, Context, Template from django.http ...

  10. 读书笔记_Effective_C++_条款二十三:宁以non-member、non-friend替换member函数

    有下面一种情况 class A { private: int a; int b; public: A(int x, int y) :a(x), b(y){} void a_display(){ cou ...