JDK 1.5 特性使用实例4--Enum
来源:互联网 发布:软件用户手册使用步骤 编辑:程序博客网 时间:2024/05/20 05:45
在JDK 1.5中,Enum有两种类型,EnumSet 和EnumMap
实例1 . Demo.java
package maoxiang.examples.jdk15.Enum;
import java.util.EnumSet;
/**
* @author 毛翔
*
* EnumSet
* EnumMap
*
*/
public class Demo {
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
public static void main(String[] args) {
Demo demo=new Demo();
demo.Test1();
}
public void Test1(){
for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))
System.out.println(d);
}
}
实例2. Card.java
package maoxiang.examples.jdk15.Enum;
import java.util.ArrayList;
import java.util.List;
/**
* @author 毛翔
*
*/
public class Card {
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public Rank rank() { return rank; }
public Suit suit() { return suit; }
public String toString() { return rank + " of " + suit; }
private static final List<Card> protoDeck = new ArrayList<Card>();
// Initialize prototype deck
static {
for (Suit suit : Suit.values())
for (Rank rank : Rank.values())
protoDeck.add(new Card(rank, suit));
}
public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
}
}
实例3.Operation.java
package maoxiang.examples.jdk15.Enum;
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } };
// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
public static void main(String args[]) {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y));
}
}
实例4 Planet.java
package maoxiang.examples.jdk15.Enum;
/**
* @author 毛翔
*
*/
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),
PLUTO (1.27e+22, 1.137e6);
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 = 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));
}
}
- JDK 1.5 特性使用实例4--Enum
- JDK 1.5 特性使用实例1--Generics
- JDK 1.5 特性使用实例2--Varargs
- JDK 1.5 特性使用实例3--AutoBoxing
- JDK 1.5 特性使用实例5--metadata
- JDK 1.5 特性使用实例6--staticimport
- JDK 1.5 特性使用实例7--运行外部程序
- Enum使用实例
- Java 1.5新特性Enum的学习和使用
- Java 1.5新特性Enum的学习和使用
- Java 1.5新特性Enum的学习和使用
- jdk 1.5 新特性 4
- JDK 1.5中的ENUM用法
- JDK 1.5 特性
- JDK 1.5新特性
- JDK 1.5 新特性
- JDK 1.5新特性
- JDK 1.5 新特性
- JDK 1.5 特性使用实例2--Varargs
- Java 多线程入门大全
- 利用 Ant 和 JUnit 进行开发
- JDK 1.5 特性使用实例3--AutoBoxing
- What is Favicon.ico? Personalise Your Site's Bookmark(zt)
- JDK 1.5 特性使用实例4--Enum
- javascript 怎样比较日期/时间的大小
- Log4J与JBuilder、Eclipse配合使用指南
- JDK 1.5 特性使用实例5--metadata
- 缩小SQL SERVER日志文件
- JDK 1.5 特性使用实例6--staticimport
- 免费申请优易普企业管理软件注册码!!!
- JDK 1.5 特性使用实例7--运行外部程序
- 免费下载优易普企业管理软件单机免费版!!!