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 ...
随机推荐
- [Qt] CFlip 翻页功能实现
由于需要给table制作翻页功能,所以写了一个翻页的类. 看上去总体效果感觉还是不错的,哈哈. //flip.h #ifndef CFLIP_H #define CFLIP_H #include &l ...
- (转)苹果推送通知服务教程 Apple Push Notification Services Tutorial
本文译自http://www.raywenderlich.com/.原文由iOS教程团队 Matthijs Hollemans 撰写,经原网站管理员授权本博翻译. 在iOS系统,考虑到手机电池电量,应 ...
- 升级Android ADT 和SDK
因为眼下从事android开发工作,所以升级了下Android SDK和eclipse ADT插件 一.更新ADT 1.Eclipse中打开Help->Install New Software. ...
- USB描述符解析-->枚举.
枚举可以理解为主机按不定的顺序向USB设备讨要设备信息,好给它分配资源,若枚举不成功,就放弃分配资源,免得浪费资源.一般都是使用中断传输方式通信. 常用的描述符有以下几种:01H.设备描述符 02H ...
- C#简单注册表操作实例
1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径 ...
- (转)VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
中调试项目遇到错误提示,Visual Studio 2010(或VS2008或VS2005)启动调试的时候,弹出提示信息: 生成下面的模块时,启用了优化或没有调试信息: C:\WINDOWS\Micr ...
- SQL Server系统表讲解
1. sysobjects http://www.cnblogs.com/atree/p/SQL-Server-sysobjects.html 2.syscomments http://www.c ...
- C#生成高清缩略图
/// <SUMMARY> /// 为图片生成缩略图 /// </SUMMARY> /// <PARAM name="phyPath">原图片的 ...
- shell中命令的执行流程
在shell中,一个命令有3中写法: 1 可以直接写(Normal Command) 2 可以放在双引号中("Command") 3 可以放在单引号中('Comand') 这3中写 ...
- shell的string operator
${varname:-word} 如果varname存在并且不为nil,那么返回varname的值,否则返回word.这个常用来在varname未定义时返回默认值 ${varname:=word} 如 ...