java - 构造函数调用必须是使用 this 的构造函数中的第一个语句

标签 java

为什么我不能使用将输入引用到第一个构造函数的 switch case 语句来创建第二个构造函数?它显示错误“构造函数调用必须是使用 this 的构造函数中的第一个语句”。所以看来我必须为第二个构造函数中的每个 case 语句重新输入第一个构造函数的赋值。

public class Card {
        public static final String CLUBS = "Clubs";
        public static final String DIAMONDS = "Diamonds";
        public static final String HEARTS = "Hearts";
        public static final String SPADES = "Spades";
        public static final int ACE = 1;
        public static final int JACK = 11;
        public static final int QUEEN = 12;
        public static final int KING = 13;

        public Card(int rank, String suit) {
            this.rank = rank;
            this.suit = suit;
        }

        public Card(String rank, String suit) {
            if (!isCorrectSuit(suit)) throw new IllegalArgumentException("incorrect suit");
            switch(rank) {
            case ACE: this(1, suit);
            case JACK: this(11, suit);
            case QUEEN: this(12, suit);
            case KING : this(13, suit);
            default: throw new IllegalArgumentException("incorrect rank");
            }
        }



        private boolean isCorrectSuit(String suit) {
            return (suit.equals(CLUBS) || suit.equals(DIAMONDS) || suit.equals(HEARTS) || suit.equals(SPADES));
        }

        private boolean isCorrectRank(int rank) {
            return rank == 1 || rank == 11 || rank == 12 || rank == 13;
        }

        private int rank;
        private String suit;    

    }

最佳答案

您可能正在寻找一个静态工厂方法,其中 this(...) 可以替换为 new Card(...):

class Card {
    ...

    private Card(int rank, String suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public static Card of(String rank, String suit) {
        if (!isCorrectSuit(suit)) {
            throw new IllegalArgumentException("incorrect suit");
        }

        final Card card;
        switch (Integer.valueOf(rank)) {
            case ACE:
                card = new Card(1, suit);
                break;
            case JACK:
                card = new Card(11, suit);
                break;
            case QUEEN:
                card = new Card(12, suit);
                break;
            case KING:
                card = new Card(13, suit);
                break;
            default: {
                throw new IllegalArgumentException("incorrect rank");
            }
        }
        return card;
    }
}

这四个实例可以预定义,无需重新创建。

<小时/>

我想知道为什么您没有对 rank 值应用相同的方法:

public Card(int rank, String suit) {
    if (!isCorrectSuit(suit)) {
        throw new IllegalArgumentException("incorrect suit");
    }

    if (!isCorrectRank(rank)) {
        throw new IllegalArgumentException("incorrect rank");
    }

    this.rank = rank;
    this.suit = suit;
}

public Card(String rank, String suit) {
    this(Integer.valueOf(rank), suit);
}

请注意,您有 2 个公共(public)构造函数,其中只有一个具有某种验证功能。我可以创建一张新卡(2, "Unknown")并且不会出现任何异常。

其他可供考虑的选项可能是:

  • 编写枚举而不是原始值
  • 编写值集以用 Set#contains 替换 isCorrectX 方法

关于java - 构造函数调用必须是使用 this 的构造函数中的第一个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49297292/

相关文章:

java - 使用Java获取MySQL表中的所有主键

java - 多个字符串以不同方式着色到 JTable 的同一单元格中

java - Android:将数据(附加)传递给 fragment

java - 将 .Java 文件转换为 .Smali 文件

java - 打印数学随机生成的数字的最大数字

Java 8 嵌套 lambda 函数查找特定键的总和

java - 经纬度 : Check whether a given point of LatLng is within a 5km radius of another point

java - 相当于Objective C中Java的DataOutputStream

java - 使用 getParent() 在 CardLayout 中的卡片之间切换

java - 如何在 Java 中修剪字符串