参考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. Shiro-授权

    把 realms 配置给SecurityManager 在认证的时候单个realm是这样配置的: <bean id="securityManager" class=" ...

  2. Android较低版本(<5.2) 页面默认Select选择框效果的BUG解决

    Bug描述: 使用低版本安卓(<5.2),在微信上打开网页,点击下拉框,会出现如下图所示的用来展示select选项的弹出框: 在选项较少的时候,可以向下滑动,将选项滑到底部 滑动前: 滑动后: ...

  3. HID Boot device.

    整理这篇文章的目的: 客户会有用到遥控器部分(遥控器操作flow:当按下某个键时,MCU会透过UR送command给TP,TP吃到后再透过微软标准的keyboard上报) 要求:在BIOS设定阶段,遥 ...

  4. Excel01-不同的单元格输入同一数据

    第一步:按住Ctrl键,选择不同的单元格 第二步:选择完最后一个单元格后,输入需要的数据“YES”,按Ctrl+Enter键结束. 提示:按Ctrl+; 输入当前日期,再按ctrl+Enter实现全部 ...

  5. fstab的格式

    # /etc/fstab/dev/hda8 swap swap defaults 0 0/dev/hda9 / ext2 defaults 1 1/dev/hda6 /wine vfat defaul ...

  6. CentOS yum Fatal Error 处理一例

    环境说明 [root@thatsit ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@thatsit ~]# uname - ...

  7. Webstorm10.0.3破解程序及汉化包下载、Webstorm配置入门指南

    核心提示: WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaSscri ...

  8. JavaScript中函数参数的按值传递与按引用传递(即按地址传递)

    首先声明一句:JavaScript中所有函数的参数都是按值传递的!不存在按引用传递! 在讲传递参数之前我们先来讲一下指针. 学过C指针的应该都知道,指针变量中保存的是一个地址,程序可以根据所保存的地址 ...

  9. Hyper-v虚拟机上网

    Windows 8中内置的Hyper-V管理器可以说给许多人带来了惊喜!在Hyper-V管理器强大的同时,也同样面临着设置中一些不可避免的麻烦.有人说,Hyper-V虚拟机联网麻烦,其实,只要掌握了技 ...

  10. 对于数据操作的SQL语句精粹(长期更新)

    --删除空格 Update [Table] Set [Column]=Replace([Column],' ','') --查出左右和右边带空格的数据 select RTRIM( LTRIM([Col ...