构造函数与类的主方法中的断言语句背后的原因是什么?

时间:2014-07-01 23:37:39

标签: java constructor main assert

I'm taking this online Java course并且不理解代码中不同断言部分背后的原因。

为什么rankToString在类的主方法和类的构造函数中的isValidRank方法中断言方法?

public class Card {
private final int rank;
private final int suit;

// Kinds of suits
public final static int DIAMONDS = 1;
public final static int CLUBS    = 2;
public final static int HEARTS   = 3;
public final static int SPADES   = 4;

// Kinds of ranks
public final static int ACE   = 1;
public final static int DEUCE = 2;
public final static int THREE = 3;
public final static int FOUR  = 4;
public final static int FIVE  = 5;
public final static int SIX   = 6;
public final static int SEVEN = 7;
public final static int EIGHT = 8;
public final static int NINE  = 9;
public final static int TEN   = 10;
public final static int JACK  = 11;
public final static int QUEEN = 12;
public final static int KING  = 13;

public Card(int rank, int suit) {
    assert isValidRank(rank);
    assert isValidSuit(suit);
    this.rank = rank;
    this.suit = suit;
}

public int getSuit() {
    return suit;
}

public int getRank() {
    return rank;
}

public static boolean isValidRank(int rank) {
    return ACE <= rank && rank <= KING;
}

public static boolean isValidSuit(int suit) {
    return DIAMONDS <= suit && suit <= SPADES;
}

public static String rankToString(int rank) {
    switch (rank) {
    case ACE:
        return "Ace";
    case DEUCE:
        return "Deuce";
    case THREE:
        return "Three";
    case FOUR:
        return "Four";
    case FIVE:
        return "Five";
    case SIX:
        return "Six";
    case SEVEN:
        return "Seven";
    case EIGHT:
        return "Eight";
    case NINE:
        return "Nine";
    case TEN:
        return "Ten";
    case JACK:
        return "Jack";
    case QUEEN:
        return "Queen";
    case KING:
        return "King";
    default:
        //Handle an illegal argument.  There are generally two
        //ways to handle invalid arguments, throwing an exception
        //(see the section on Handling Exceptions) or return null
        return null;
    }    
}

public static String suitToString(int suit) {
    switch (suit) {
    case DIAMONDS:
        return "Diamonds";
    case CLUBS:
        return "Clubs";
    case HEARTS:
        return "Hearts";
    case SPADES:
        return "Spades";
    default:
        return null;
    }    
}

public static void main(String[] args) {

    // must run program with -ea flag (java -ea ..) to
    // use assert statements
    assert rankToString(ACE) == "Ace";
    assert rankToString(DEUCE) == "Deuce";
    assert rankToString(THREE) == "Three";
    assert rankToString(FOUR) == "Four";
    assert rankToString(FIVE) == "Five";
    assert rankToString(SIX) == "Six";
    assert rankToString(SEVEN) == "Seven";
    assert rankToString(EIGHT) == "Eight";
    assert rankToString(NINE) == "Nine";
    assert rankToString(TEN) == "Ten";
    assert rankToString(JACK) == "Jack";
    assert rankToString(QUEEN) == "Queen";
    assert rankToString(KING) == "King";

    assert suitToString(DIAMONDS) == "Diamonds";
    assert suitToString(CLUBS) == "Clubs";
    assert suitToString(HEARTS) == "Hearts";
    assert suitToString(SPADES) == "Spades";

    }
}

2 个答案:

答案 0 :(得分:3)

assert方法中的main测试关于静态最终字段的断言,构造函数中的assert测试关于非静态最终字段的断言。由于静态字段的断言不应该在每次创建对象时进行测试,而非静态字段需要一个实例,因此将断言置于那里是有意义的。

但是我建议编写jUnit测试而不是使用assert s。


您会找到tutorial for jUnit at vogella.com

jUnit的一些好处:

  • 您不必将测试与代码混合在一起,这使代码更具可读性。
  • 您可以测试函数调用的序列等。而不改变代码的行为(例如,如果在没有向代码添加try-catch块的情况下在断言中抛出异常,则很难测试)
  • 大多数IDE为jUnit
  • 提供了一个很好的用户界面
  • 检查断言时,您可以更好地控制。例如。你可以创建2 Card而无需在构造函数中检查断言两次。

答案 1 :(得分:-1)

assert是一个java关键字,可让您测试某些条件(并强制它有效)。这里使用的格式是assert <boolean> 在两种情况下,您都会看到assert后面的表达式评估为boolean

您可以在this Oracle documentation中了解更多相关信息。