创建一个表示扑克牌的类

时间:2013-09-18 13:01:57

标签: java

我正在尝试制作一个Card类来代表扑克牌。底部的代码运行顶部的代码。我只是需要帮助入门。我不太确定如何。我如何为这个类创建构造函数?

public class Card
{
  public static final String FACES[] = {"ZERO","ACE","TWO","THREE","FOUR",
  "FIVE","SIX","SEVEN","EIGHT","NINE","TEN","JACK","QUEEN","KING"};

//instance variables
    //String suit
    //int face

//constructors


// modifiers
    //set methods


//accessors
    //get methods


//toString

 }

public class CardRunner
{
    public static void main( String args[] )
    {
    Card one = new Card("SPADES", 9);
    out.println(one.getSuit());
    out.println(one.getFace());

    Card two = new Card("DIAMONDS", 1);
    out.println(two);
    two.setFace(3);
    out.println(two);

    Card three = new Card("CLUBS", 4);
    out.println(three);

    Card four = new Card("SPADES", 12);
    out.println(four);

    Card five = new Card("HEARTS", 12);
    out.println(five);
}
}

1 个答案:

答案 0 :(得分:4)

我认为您应该在enum使用PlayingCard,因为所有这些都是常量,您可以覆盖toString以提供有意义的表示。

如果创建枚举,则不必显式创建对象,并且可以在需要时使用枚举常量。您可以阅读why to use enumerations in java

上的精彩教程