PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   七嘴八舌異言堂 (https://www.pcdvd.com.tw/forumdisplay.php?f=12)
-   -   java 裡的 enum type (https://www.pcdvd.com.tw/showthread.php?t=657909)

risc_bar 2006-09-28 08:59 AM

DEUCE, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE 這些是String 還是你自己定義的class

doberman 2006-09-28 09:05 AM

定義了Suit, Rank這些variable type,後面method裡也是得回傳object裡的這些variable type.

改完試試看不知有沒好?

/*------------------------------------------
public Card(Suit suit, Rank rank, int value)
{
this.suit = suit;
this.rank = rank;
this.value = value;
}

/**
* Get the suit of the card
* @return String
*/
public Suit getSuit()
{
return this.suit;
}

/**
* Get the rank of the card
* @return String
*/
public Rank getRank()
{
return this.rank;
}

/**
* Get the actual value of the card
* @return int
*/
public int getValue()
{
return this.value;
}



}
-----------------------------*/

gary0718 2006-09-28 09:17 AM

參考一下
http://www-128.ibm.com/developerwor...tiger-eclispe1/

青年阿要立死志 2006-09-28 09:23 AM

private Rank rank;
private Suit suit;
private int value;

這三行就怪怪的
enum類別有final特性
你這樣定義..?

再來
compile error是:

Card.java:39: incompatible types
found : java.lang.String
required: Card.Suit
this.suit = suit;
^
Card.java:40: incompatible types
found : java.lang.String
required: Card.Rank
this.rank = rank;
^
Card.java:50: incompatible types
found : Card.Suit
required: java.lang.String
return this.suit;
^
Card.java:59: incompatible types
found : Card.Rank
required: java.lang.String
return this.rank;
^
4 errors


你的getSuit,getRank
return type是String
getValue是 int

而enum的值根據書本
不可是int或String

可能可以用valueOf()來取得和名稱相同的值
如Rank.valueOf("DEUCE")

不保證對喔

Xforce 2006-09-28 09:28 AM

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 Rank rank; // the suit of a card
private Suit suit; // the rank of a card
private int value; // the actual value of a card

/**
* Class constructor
* The default Card constructor, creates a new Card object with default values.
*/
public Card()
{
suit = null;
rank = null;
value = 0;
}

/**
* Class constuctor
* Takes the suit, rank and value the card and create a Card object.
* The suit argument must be in either spade, heart, diamond or club. The
* value of rank argument must be chosen from Ace to K. The value argument must
* be an integer from 1 to 13.
* <p>
* One Card object will be created with the given parameters.
*
* @param suit the suit of the card
* @param rank the rank of the card
* @param value the value of the card
*/
public Card(String suit, String rank, int value)
{
this.suit = Suit.valueOf(suit);
this.rank = Rank.valueOf(rank);
this.value = value;
}

/**
* Get the suit of the card
* @return String
*/
public String getSuit()
{
return this.suit.toString();
}

/**
* Get the rank of the card
* @return String
*/
public String getRank()
{
return this.rank.toString();
}

/**
* Get the actual value of the card
* @return int
*/
public int getValue()
{
return this.value;
}

}
先搞清楚enum 怎麼用~~


所有的時間均為GMT +8。 現在的時間是10:02 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2026。