数据结构与算法分析-第一章Java类(03)

编写一个Java枚举Rank表示扑克牌的点数,包括ACE,TWO,THREE,FOUR,FIVE,FIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING。定义一个私有数据域以保存一个布尔值,当扑克牌为人脸(JACK,QUEEN,KING)时为真。定义一个构造函数以初始化该域,一个访问者方法返回该域的值,以及一个toString方法。

编写一个测试程序演示该枚举

package com.crazycoder2010.datastructure.ch01;

public enum Rank {
	ACE(false), TWO(false), THREE(false), FOUR(false), FIVE(false), SIX(false), SEVEN(
			false), EIGHT(false), NINE(false), TEN(false), JACK(true), QUEEN(
			true), KING(true);
	private boolean face;

	private Rank(boolean face) {
		this.face = face;
	}
	public boolean isFace() {
		return face;
	}

	@Override
	public String toString() {
		return this.name() + ",face:" + this.face;
	}
}
测试程序

package com.crazycoder2010.datastructure.ch01;

public class RankTest {
	public static void main(String[] args) {
		//toString test
		System.out.println(Rank.ACE);
		System.out.println(Rank.JACK);
		//access
		System.out.println(Rank.EIGHT.isFace());//false
		System.out.println(Rank.KING.isFace());//true
		for(Rank rank : Rank.values()){
			System.out.print(rank.name()+";");
		}
	}
}
输出结果:

ACE,face:false
JACK,face:true
false
true
ACE;TWO;THREE;FOUR;FIVE;SIX;SEVEN;EIGHT;NINE;TEN;JACK;QUEEN;KING;



你可能感兴趣的:(java,数据结构,算法,String,测试,2010)