Java Tutorial/Data Type/enum
Содержание
- 1 Comparing Enumeration Values
- 2 Enumeration Fundamentals
- 3 Enums in a Class
- 4 Enum type field
- 5 enum type with its own method
- 6 enum with switch
- 7 equals and = operator for enum data type
- 8 How to define an enumeration
- 9 Two enumeration constants can be compared for equality by using the == relational operator
- 10 uses an enum, rather than interface variables, to represent the answers.
Comparing Enumeration Values
public class MainClass {
enum Season {
spring, summer, fall, winter
};
public static void main(String[] arg) {
Season season = Season.summer;
if (season.equals(Season.spring)) {
System.out.println("It is Spring.");
} else {
System.out.println("It isn\"t Spring!");
}
}
}
It isn"t Spring!
Enumeration Fundamentals
An enumeration is created using the new enum keyword.
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
Value of aWeekDay: Monday
Enums in a Class
public class Shape {
private enum ShapeType {
RECTANGLE, TRIANGLE, OVAL
};
private ShapeType type = ShapeType.RECTANGLE;
public String toString() {
if (this.type == ShapeType.RECTANGLE) {
return "Shape is rectangle";
}
if (this.type == ShapeType.TRIANGLE) {
return "Shape is triangle";
}
return "Shape is oval";
}
}
Enum type field
public class ShirtTest {
public static void main(String[] args) {
Shirt shirt1 = new Shirt();
shirt1.setName("new name");
shirt1.setBid(23.5);
shirt1.setSize(Size.M);
System.out.println(shirt1);
}
}
class Shirt {
private String name;
private double bid;
private Size size;
public Shirt() {
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setBid(double bid) {
this.bid = bid;
}
public double getBid() {
return bid;
}
public void setSize(Size size) {
this.size = size;
}
public Size getSize() {
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" Name: " + this.getName() + ",");
sb.append(" Bid: " + this.getBid() + " Dollar,");
sb.append(" Size: " + this.getSize());
return sb.toString();
}
}
enum Size {
S, M, L, XL, XXL, XXXL;
}
enum type with its own method
public class SizeIterator {
public static void main(String[] args) {
Size[] sizes = Size.values();
for (Size s : sizes) {
System.out.println(s);
}
}
}
enum Size implements Countable {
S, M, L, XL, XXL, XXXL;
@Deprecated
public Size increase() {
Size sizes[] = this.values();
int pos = this.ordinal();
if (pos < sizes.length - 1)
pos++;
return sizes[pos];
}
}
interface Countable {
public Size increase();
}
enum with switch
public class SizeSwitch {
public static void main(String[] args) {
Size size = Size.XL;
switch(size){
case S:
System.out.println("S");
break;
case M:
System.out.println("M");
break;
case L:
System.out.println("L");
break;
case XL:
System.out.println("XL");
break;
case XXL:
System.out.println("XXL");
break;
case XXXL:
System.out.println("XXXL");
break;
}
}
}
enum Size {
S, M, L, XL, XXL, XXXL;
}
equals and = operator for enum data type
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
Week day1, day2, day3;
day1 = Week.Monday;
day2 = Week.Monday;
day3 = Week.Monday;
if(day1.equals(day2))
System.out.println("Error!");
if(day1.equals(day3))
System.out.println(day1 + " equals " + day3);
if(day2 == day3)
System.out.println(day2 + " == " + day3);
}
}
Error! Monday equals Monday Monday == Monday
How to define an enumeration
- To define a new type, Day.
- Variable of type Day can only store the values specified between the braces.
- Monday, Tuesday, ... Sunday are called enumeration constants.
- These names will correspond to integer values, starting from 0 in this case.
public class MainClass {
enum Day {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
public static void main(String[] args) {
Day yesterday = Day.Thursday;
Day today = Day.Friday;
Day tomorrow = Day.Saturday;
System.out.println("Today is " + today);
System.out.println("Tomorrow will be " + tomorrow);
System.out.println("Yesterday was " + yesterday);
}
}
Today is Friday Tomorrow will be Saturday Yesterday was Thursday
Two enumeration constants can be compared for equality by using the == relational operator
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class MainClass {
public static void main(String args[]) {
Week aWeekDay;
aWeekDay = Week.Monday;
// Output an enum value.
System.out.println("Value of aWeekDay: " + aWeekDay);
System.out.println();
aWeekDay = Week.Friday;
// Compare two enum values.
if (aWeekDay == Week.Friday)
System.out.println(" Friday.\n");
}
}
Value of aWeekDay: Monday Friday.
uses an enum, rather than interface variables, to represent the answers.
import java.util.Random;
enum Answers {
NO, YES, MAYBE, LATER, SOON, NEVER
}
class Question {
Random rand = new Random();
Answers ask() {
int prob = (int) (100 * rand.nextDouble());
if (prob < 15)
return Answers.MAYBE; // 15%
else if (prob < 30)
return Answers.NO; // 15%
else if (prob < 60)
return Answers.YES; // 30%
else if (prob < 75)
return Answers.LATER; // 15%
else if (prob < 98)
return Answers.SOON; // 13%
else
return Answers.NEVER; // 2%
}
}
class AskMe {
static void answer(Answers result) {
switch (result) {
case NO:
System.out.println("No");
break;
case YES:
System.out.println("Yes");
break;
case MAYBE:
System.out.println("Maybe");
break;
case LATER:
System.out.println("Later");
break;
case SOON:
System.out.println("Soon");
break;
case NEVER:
System.out.println("Never");
break;
}
}
public static void main(String args[]) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}