Enum Types
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的更多相关文章
- 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 ...
- Java中Enum类型的序列化(转)
在Java中,对Enum类型的序列化与其他对象类型的序列化有所不同,今天就来看看到底有什么不同.下面先来看下在Java中,我们定义的Enum在被编译之后是长成什么样子的. Java代码: Java代码 ...
- 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 ...
- Javac语法糖之Enum类
枚举类在Javac中是被当作类来看待的. An enum type is implicitly final unless it contains at least one enum constant ...
- [Java面经]干货整理, Java面试题(覆盖Java基础,Java高级,JavaEE,数据库,设计模式等)
如若转载请注明出处: http://www.cnblogs.com/wang-meng/p/5898837.html 谢谢.上一篇发了一个找工作的面经, 找工作不宜, 希望这一篇的内容能够帮助到大 ...
- Entity Framework 教程——模型浏览器
模型浏览器: 在之前的章节中,我们创建了第一个关于学校的实体数据模型.但是EDM设计器并没有将他所创建的所有对象完全显示出来.它只将数据库中的被选择的表与视图显示出来了. 模型浏览器可以将EDM所创建 ...
- 模型浏览器【Model Browser】【EF基础系列6】
We have created our first Entity Data Model for School database in the previous section. The visual ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- BookRent借阅管理
最近整了个BookRent的小应用,单机版.连本地sqlite db.wpf界面,其中涉及到一些有趣的小功能和小坑,简单小结一下. 项目结构是wpf ui->view model->rep ...
随机推荐
- 【KMP】Period
KMP算法 Next[]函数深入理解,Next[]当前字符前匹配字符数,串长n-Next[i]=串内循环子串的长度p. 本题求子循环串内循环节数. Problem Description For ea ...
- 【转】在ASP.NET MVC中,使用Bundle来打包压缩js和css
在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...
- es6新特性:
http://es6katas.org/ es6+一些新特性,截图如下 对应方法,函数显示相关的数据,如图: 对应方法,函数的例子,如下
- [转载]SQL Server查找包含某关键字的存储过程3种方法
存储过程都写在一个指定的表中了,我们只要使用like查询就可以实现查询当前这台SQL Server中所有存储过程中包括了指定关键字的存储过程并显示出来,下面一起来看看我总结了几条命令. 例子1 代码如 ...
- NSUserDefaults读取和写入自定义对象
NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...
- django: template variable
模板变量用双大括号显示,如: <title>page title: {{title}}</title> 一 模板中使用变量 继续前面的例子,修改 index.html: < ...
- Lesson 2: Dive Into Typography (排版)
Lesson 2: Dive Into Typography (排版) 排版是字的艺术,是关于字的一切:字体.字号.行高.行长.字重(斜体/加粗/正常).字距(kerning).行距(leading) ...
- IPointCollection,ISegmentCollection和IGeometryCollection
Engine 提供了三个主要的几何图形集合接口用于对几何对象的操作,分别是 IPointCollection,ISegmentCollection 和 IGeometryCollection,这些接口 ...
- oracle获得每周,每月,每季度,每年的第一天
当前年月日 SELECT trunc(sysdate) , trunc(sysdate,'dd') FROM dual 当年第一天 SELECT trunc(sysdate,'yyyy') FRO ...
- OC基础 文件管理
OC基础 文件管理 1.文件管理类NSFileManager对象的创建: NSFileManager *fm = [NSFileManager defaultManager]; 2.文件操作: (1 ...