我认为我的问题主要是语法,但可能是我对类层次结构的整体理解。基本上它是一个 Deck 类,其中包含一个填充了 Card 对象的数组,Card 是 Deck 的子类,所以 Deck 应该能够使用 Card 的块和方法,对吧?如果是这样,我会在试图调用它时把语法弄得一团糟。我正在使用嵌套的 while 循环来填充数组,但我希望 Card 对象的每个实例都具有该卡的花色和等级,而不是仅仅打印“a Card”。我离开了我试图让 Card 对象成为另一个大小为 2 的数组来保存 Suit 和 Rank 的地方,但是我的 gst 编译器说它需要一个“对象”,所以很明显我做错了什么。我粘贴了我的代码,这样你就可以看到我在说什么。
"The Deck object class is a deck of 52 Card objects. "
Object subclass: Deck [
| Content |
<comment: 'I represent of Deck of Cards'>
Deck class >> new [
<category: 'instance creation'>
| r |
r := super new .
Transcript show: 'start ' .
r init .
^r
]
init [
<category: 'initialization'>
|a b c|
Content := Array new: 52 .
a := 1 .
c := 1 .
[a <= 4] whileTrue:[
b := 1 .
[b <= 13] whileTrue:[
|card|
card := Card new .
Card := Array new: 2 . "This is where I'm trying to use the Card class blocks to make the Card objects have Rank and Suit"
Card at: 1 put: Card setRank: b| . "and here the rank"
Card at: 2 put: Card getSuit: a| . "and the suit"
Content at: c put: card .
b := b + 1 .
c := c + 1 .
].
a := a + 1 .
].
Content printNl .
]
] .
"The Card object has subclass Suit and a FaceValue array of Suit and Rank. "
Object subclass: Card [
| Suit Rank |
<comment: 'I represent a playing Card' >
init [
<category: 'initialization'>
Suit := Club .
Rank := 1 .
Transcript show: 'nCard ' .
^super init
]
getSuit: suitVal [
suitVal = 1 ifTrue: [Suit := Club] .
suitVal = 2 ifTrue: [Suit := Diamond] .
suitVal = 3 ifTrue: [Suit := Heart] .
suitVal = 4 ifTrue: [Suit := Spade] .
^Suit
] "getSuit"
setRank: rankVal [
Rank := rankVal .
^Rank
]
]
z := Deck new .