2015-10-20 110 views
-2

我做我的功课时有点困难,我很难理解下一步该做什么。所以我的指令如下:在默认的构造函数初始化属性

  1. 创建一个名为Card新类。超类的构造器复选框将在代码中创建一个构造器存根。

  2. 与价值观NONECLUBSHEARTSSPADESDIAMONDS称为Suit新的源文件创建一个枚举类型。

  3. 在一个新的源文件中的同一个包中Card类创建枚举类型,称为Rank与值JOKERTWOTHREEFOURFIVESIXSEVENEIGHTNINETENJACKQUEENKING,ACE

  4. 在类Card
    A)创建一个类属性(字段)称为Rank类型的rank
    B)创建一个类型为Suit的类属性(字段)suit
    C)为这些字段创建getter和setter。使setter私人化,因为我们只希望构造函数设置卡片SuitRank(例如,我们不希望程序员或用户设置多于一个的ACE)。

  5. 创建一个构造函数,接受SuitRank并使用这些值来设置属性。

  6. 在默认构造函数中,初始化suitrank属性。使用此构造函数调用在上一步中创建的构造函数,并使用您选择的值初始化排名和适合度(例如:Rank.ACE,Suit.CLUBS)。

  7. 在该类的主要方法中,构造默认的Card以演示默认的构造函数初始化。

  8. 也使用SuitRank构造函数构造一些额外的卡。使用局部变量来保存对象。

  9. 从枚举类型使用toString()方法,打印创建的每个CardSuitRank

  10. 创建一个Deck类,该类包含一个由五十四张卡片(Containment)组成的数组的字段。

  11. 上创建Deck类初始化的与标准的甲板和两个王牌卡的阵列,使用for循环,即遍历SuitRank值并用适当的Card构造设置的阵列成员的构造函数。请记住,两个Jokers只存在为NONE套装,而只有Jokers使用NONE套装。有很多可能的解决方案只创建两个jokers。我使用了一个迭代器,将它们排除在循环之外,并在开始时单独添加它们。使用if语句排除for each循环内的jokers,并在之前或之后添加它们。

  12. Deck类创建toString()方法,使用另一个for循环通过调用每个卡上的toString()方法打印所有Card在数组英寸此方法“询问”Card仅通过在Card对象上调用toString()来打印其等级和套装。在此方法中的任何地方都不应该提及RankSuit。让CardtoString()方法做工作(委托)。

  13. 在创建DeckDeck类中创建main方法。然后使用System.out.println()打印纸牌。这将调用DecktoString()方法,该方法将在每个Card上调用toString(),打印整个卡组。

public class Card { 

    //Default Constructor 
    Card(){ 
    } 

    //Create a class attribute (field) called rank of type Rank. 
    Rank rank; 
    //Create a class attribute (field) called rank of type Suit 
    Suit suit; 
    //Create an Enumerated Type in a new source file called Suit 
    public enum Suit{ 
    NONE, CLUBS, HEARTS, SPADES, DIAMONDS 
    } 
    //Create an Enumerated Type in a new source file called Rank 
    public enum Rank{ 
    JOKER, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK,QUEEN,KING, ACE   
    } 

    //Getters & Setters for Rank/Suit 
//Make the setters private, since we only want the constructor to set the card Suit and Rank 
public Rank getRank() { 
return rank; 
} 
private void setRank(Rank rank) { 
this.rank = rank; 
} 
public Suit getSuit() { 
return suit; 
} 
private void setSuit(Suit suit) { 
this.suit = suit; 
} 

//Create a constructor that accepts a Suit and a Rank and use those values to set the attributes. 
Card (Rank Rank, Suit Suit){ 
suit = Suit; 
rank = Rank; 
} 






    public static void main(String[] args) { 

    } 



    } 
+1

SO不这样工作。不要指望你可以复制和粘贴你的任务和你的代码;而且我们花了我们的空闲时间来阅读所有这些内容,以确定你卡在哪里;你的问题是什么......等等。坦率地说:忘了那个。描述你到目前为止所尝试的是什么以及你被困在哪里。 – GhostCat

+0

好,所以我认为我完成了正确的步骤4/5。我只需要帮助理解,如果我做对了第5步,有人解释第6步要我做什么?我只是把整个事情作为一个整体给出了一个更好的想法,总结了需要完成的工作以及之前我可能搞砸了的步骤。 – BerlinUnited

+0

获得者和制定者究竟需要什么? – BerlinUnited

回答

0

构造被用来初始化一个对象的一个​​实例。 Setters用于设置变量的值,getter返回这个值。看看下面的例子,

public class Test { 

    //private members of the instance 
    private String strVariable; 
    private int intVariable; 

    //constructor 
    //constructor sets the values of strVariable and intVariable when a Test object is intialized 
    public Test(String strVariable, String intVariable) { 

     this.strVariable = strVariable; 
     this.intVariable = intVariable; 
    } 

    //setter method for setStrVariable 
    //this will overwrite the value of setStrVariable that was assinged when the object was intilized 
    public void setStrVariable(String strVariable) { 

     this.strVariable = strVariable; 
    } 

    //getter for the setStrVariable 
    //this will return the value of setStrVariable 
    public String getStrVariable() { 

     return this.strVariable; 
    } 
} 
相关问题