[英]In the default constructor initialize attributes
I'm kind of stuck while doing my homework and I'm having a hard time to understand what to do next.我在做作业时有点卡住了,我很难理解接下来要做什么。 So my instructions are as follows:
所以我的指示如下:
Create a new class called
Card
.创建一个名为
Card
的新类。 The Constructors from superclass check box will create a constructor stub in the code.Constructors from superclass 复选框将在代码中创建一个构造函数存根。
Create an Enumerated Type in a new source file called
Suit
with the valuesNONE
,CLUBS
,HEARTS
,SPADES
,DIAMONDS
.在名为
Suit
的新源文件中创建一个枚举类型,其值为NONE
、CLUBS
、HEARTS
、SPADES
、DIAMONDS
。Create an Enumerated Type in a new source file in the same package as the
Card
class, calledRank
with the valuesJOKER
,TWO
,THREE
,FOUR
,FIVE
,SIX
,SEVEN
,EIGHT
,NINE
,TEN
,JACK
,QUEEN
,KING
,ACE
.在与
Card
类相同的包中的新源文件中创建一个枚举类型,称为Rank
,其值为JOKER
、TWO
、THREE
、FOUR
、FIVE
、SIX
、SEVEN
、EIGHT
、NINE
、TEN
、JACK
、QUEEN
、KING
、ACE
.In the class
Card
,在
Card
类中,
A) Create a class attribute (field) calledrank
of typeRank
.A) 创建一个名为
rank
的类属性(字段),其类型为Rank
。
B) Create a class attribute (field) calledsuit
of typeSuit
.B) 创建一个类型为
Suit
名为suit
的类属性(字段)。
C) Create getters and setters for these fields.C) 为这些字段创建 getter 和 setter。 Make the setters private, since we only want the constructor to set the card
Suit
andRank
(we would not want a programmer or user to set more than oneACE
ofCLUBS
for instance).将 setter 设为私有,因为我们只希望构造函数设置卡片
Suit
和Rank
(例如,我们不希望程序员或用户设置多个CLUBS
ACE
)。Create a constructor that accepts a
Suit
and aRank
and use those values to set the attributes.创建一个接受
Suit
和Rank
的构造函数,并使用这些值来设置属性。In the default constructor, initialize the
suit
andrank
attributes.在默认构造函数中,初始化
suit
和rank
属性。 Use the this constructor to call the constructor created in the previous step and initialize the rank and suit with a value of your choice (for example:Rank.ACE
,Suit.CLUBS
).使用 this 构造函数调用在上一步中创建的构造函数,并使用您选择的值(例如:
Rank.ACE
、Suit.CLUBS
)初始化等级和花色。In the main method of the class, construct a default
Card
to demonstrate the default constructor initialization.在类的main方法中,构造一个默认的
Card
来演示默认构造函数的初始化。Also construct a few additional cards using the
Suit
andRank
constructor.还可以使用
Suit
和Rank
构造函数构造一些额外的卡片。 Use local variables to hold the objects.使用局部变量来保存对象。
Using the
toString()
method from Enumerated Types, print theSuit
andRank
of eachCard
created.使用枚举类型中的
toString()
方法,打印创建的每张Card
的Suit
和Rank
。Create a
Deck
class that contains a field that is an array of fifty four cards (Containment).创建一个
Deck
类,该类包含一个字段,该字段是一个包含 54 张卡片的数组(包含)。Create a constructor on the
Deck
class that initializes the array of cards with the standard deck and two jokers, usingfor
loops that iterate over theSuit
andRank
values and set the array members with the appropriateCard
constructor.创建在一个构造
Deck
初始化与标准甲板和两个王牌卡的数组类,使用for
循环,即遍历Suit
和Rank
值,并设定阵列成员与相应的Card
构造函数。 Remember that the two Jokers only exist asNONE
suits and only the Jokers use theNONE
suit.请记住,两个 Jokers 仅作为
NONE
套装存在,并且只有 Jokers 使用NONE
套装。 There are many possible solutions for creating only two jokers.有许多可能的解决方案只创建两个小丑。 I used an iterator, left them out of the loop and added them in the beginning, individually.
我使用了一个迭代器,将它们排除在循环之外,并在开始时单独添加它们。 Use an
if
statement to exclude the jokers within afor each
loop and add them before or after.使用
if
语句排除for each
循环中的小丑并在之前或之后添加它们。Create a
toString()
method in theDeck
class, using anotherfor
loop to print all theCard
s in the array by calling thetoString()
method on each card.在
Deck
类中创建一个toString()
方法,使用另一个for
循环通过在每张卡片上调用toString()
方法来打印数组中的所有Card
。 This method “asks” theCard
to print its rank and suit by only callingtoString()
on theCard
object.此方法仅通过调用
Card
对象上的toString()
来“要求”Card
打印其等级和花色。 There should be no reference toRank
orSuit
anywhere in this method.在此方法中的任何地方都不应提及
Rank
或Suit
。 LetCard
'stoString()
method do the work (Delegation).让
Card
的toString()
方法完成工作(委托)。Create a
main
method in theDeck
class that creates aDeck
.创建一个
main
的方法Deck
,创建一个类Deck
。 Then print the deck usingSystem.out.println()
.然后使用
System.out.println()
打印牌组。 This will callDeck
'stoString()
method, which will calltoString()
on eachCard
, printing the entire deck.这将调用
Deck
的toString()
方法,该方法将在每张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) {
}
}
Constructor is used to initialised a instance of a object.构造函数用于初始化对象的实例。 Setters are used to set the values of the variables and getters return this.
Setter 用于设置变量的值,getter 返回此值。 Have a look at the example below,
看看下面的例子,
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.