我只为我的最终变量分配了一个值,但我仍然收到错误

时间:2015-06-05 14:07:59

标签: java class final

我在制作我的实例时遇到错误Strings cardRank和cardSuit final,我认为我只在我的代码中为它们分配一次值。 我已经评论过我在代码中谈论的内容。

import java.util.Arrays;

public class Card{

protected static final String[] SUITS = {"spades", "clubs", "hearts", "diamonds"};
protected static final String[] RANKS = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"};

private String cardRank; // I want to make this final
private String cardSuit; // I want to make this final

public Card(){
    this(RANKS[(int) (Math.random() * RANKS.length)] , SUITS[(int) (Math.random() * SUITS.length)]);
}

public Card(String cardRank, String cardSuit){
    setRank(cardRank);
    setSuit(cardSuit);
}

private boolean isValidRank(String cardRank){
    cardRank = cardRank.toUpperCase();
    return Arrays.asList(RANKS).contains(cardRank);
}

private boolean isValidSuit(String cardSuit){
    cardSuit = cardSuit.toLowerCase();
    return Arrays.asList(SUITS).contains(cardSuit);
}

private void setRank(String cardRank){
    if(isValidRank(cardRank)) this.cardRank = cardRank;
    else this.cardRank = "Invalid Suit";
}

private void setSuit(String cardSuit){
    if(isValidSuit(cardSuit)) this.cardSuit = cardSuit;
    else this.cardSuit = "Invalid Suit";
}

public String getRank(){
    return cardRank;
}

public String getSuit(){
    return cardSuit;
}

public void printCard(){      
    System.out.println(getSuit() + " " + getRank());
}

}

2 个答案:

答案 0 :(得分:3)

您只能 分配给构造函数体中的final字段(或作为声明的一部分或实例初始值设定项)。基本上,你应该放弃setSuitsetRank方法。

public Card(String cardRank, String cardSuit) {
    this.cardRank = isValidCardRank(cardRank) ? "Invalid rank";
    this.cardSuit = isValidCardSuit(cardSuit) ? "Invalid suit";
}

或更好:

public Card(String cardRank, String cardSuit) {
    this.cardRank = validateRank(cardRank);
    this.cardSuit = validateSuit(cardSuit);
}

如果有效无效,validate*方法会抛出IllegalArgumentException

或者更好的是,设置SuitRank枚举,因此值是有效值,您只需要检查无效...

答案 1 :(得分:0)

如果您想使用enum,以下是您的卡片类的示例。不需要验证,因为可通过的参数仅在定义的enum内。由于RANK和SUIT是最终的,因此您不需要为它们设置方法。

public class Card {

    public enum SUIT {SPADE, CLUB, HEART, DIAMOND};

    public enum RANK {
        ACE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), TEN("10"), JACK("J"), QUEEN("Q"), KING("K");

        private String value;

        private RANK(String value) {
            this.value = value;
        }

        public String getValue() {
            return this.value;
        }
    };


    private final SUIT cardSuit;
    private final RANK cardRank;

    /**
     * Creates a Specific Card
     */
    public Card(SUIT cardSuit, RANK cardRank) {
        this.cardSuit = cardSuit;
        this.cardRank = cardRank;
    }

    /**
     * Generate a Random Card
     */
    public Card(){
        this(RANK.values()[(int) (Math.random() * RANK.values().length)] , SUIT.values()[(int) (Math.random() * SUIT.values().length)]);
    }

    public String getSuit() {
        return cardSuit.toString();
    }

    public String getRank() {
        return cardRank.getValue();
    }

    public void printCard() {
        System.out.println(getSuit() + " " + getRank());
    }

}