JDK 1.5 Characteristics Example 4 - ENUM

xiaoxiao2021-03-06  74

In JDK 1.5, ENUM has two types, enumset and enummap

Example 1. Demo.java

Package maoxiang.examples.jdk15.enum;

Import java.util.enumset;

/ **

* @Author Mao Xiang

*

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

}

}

Example 2. Card.java

Package maoxiang.examples.jdk15.enum;

Import java.util.arraylist;

Import java.util.list;

/ **

* @Author Mao Xiang

*

* /

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;

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 protodeck = new arraylist ();

// Initialize Prototype Deck

STATIC {

Suit suit: suit.values ​​())

For (Rank Rank: rank.values ​​())

Protodeck.Add (New Card (Rank, Suit);

}

Public static arraylist newdeck () {

Return New ArrayList (Protodeck); // Return Copy of Prototype Deck

}

}

Example 3. Howeration.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 reported 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));

}

}

Example 4 Planet.java

Package maoxiang.examples.jdk15.enum;

/ **

* @Author Mao Xiang

*

* /

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 () {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));

}

}

转载请注明原文地址:https://www.9cbs.com/read-108568.html

New Post(0)