如何在 Ruby 类之间传递变量?
我正在创建一个具有多个类别的纸牌游戏。目前,我使用全局变量来保存 $shuffled_deck
、$players_hand
和 $dealers_hand
变量,但在使用全局变量时我担心(也许,不必要)并且更喜欢使用实例变量。
我一直在阅读周围的内容,但没有什么是真正令人感兴趣的。谁能帮我指出正确的方向?
使用实例变量,我无法保存 @players_hand
和 @dealers_hand
以便能够在其他类中使用它们。例如,我有来自 Player
类的 @players_hand
。我让 Dealer
类抽一张牌,但我无法将 @players_hand
拉入 Dealer
类中以将两者添加在一起。
我当前的代码是:
class Blackjack
def initialize
@player = Player.new
@dealer = Dealer.new
end
end
class Dealer
def initialize
@deck = Deck.new
$dealers_hand = 0
end
def hit_dealer
@deck.hit_dealer
end
def hit_player
@deck.hit_player
end
def draw_card
@hit = $shuffled_deck
end
def shuffle
@deck.suits
end
end
class Player
def initialize
$players_hand = 0
end
end
class Deck
def suits
#code that shuffled the deck..
$shuffled_deck = @shuffled_deck
end
def hit_player
@hit = $shuffled_deck.pop
end
def hit_dealer
@hit = $shuffled_deck.pop
end
end
I'm creating a card game with multiple classes. Currently, I'm using global variables to hold the $shuffled_deck
, $players_hand
, and $dealers_hand
variables, but I worry when using global variables (perhaps, needlessly) and would prefer to use instance variables.
I've been reading around, but nothing is really clicking. Can anyone help point me in the right direction with this?
Using instance variables I haven't been able to save the @players_hand
and @dealers_hand
to be able to use them in other classes. For instance, I have @players_hand
from the Player
class. I have the Dealer
class draw a card, but I can't pull that @players_hand
into the Dealer
class to add the two together.
My current code is:
class Blackjack
def initialize
@player = Player.new
@dealer = Dealer.new
end
end
class Dealer
def initialize
@deck = Deck.new
$dealers_hand = 0
end
def hit_dealer
@deck.hit_dealer
end
def hit_player
@deck.hit_player
end
def draw_card
@hit = $shuffled_deck
end
def shuffle
@deck.suits
end
end
class Player
def initialize
$players_hand = 0
end
end
class Deck
def suits
#code that shuffled the deck..
$shuffled_deck = @shuffled_deck
end
def hit_player
@hit = $shuffled_deck.pop
end
def hit_dealer
@hit = $shuffled_deck.pop
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用你的例子你可以这样做
using your example you can do it like this
您想要使用
attr_reader
、attr_writer
或attr_accessor
。它们的工作原理如下:attr_reader :players_hand
:允许您编写some_player.players_hand
来获取该玩家的players_hand
实例变量attr_writer :players_hand
:允许您编写some_player.players_hand = 0
将变量设置为 0attr_accessor :players_hand
:允许您读取和写入,就像您同时使用了attr_reader
和attr_writer
一样。顺便说一下,这些所做的只是为您编写方法。如果你愿意,你可以像这样手动完成:
You want to use
attr_reader
,attr_writer
, orattr_accessor
. Here's how they work:attr_reader :players_hand
: Allows you to writesome_player.players_hand
to get the value of that player'splayers_hand
instance variableattr_writer :players_hand
: Allows you to writesome_player.players_hand = 0
to set the variable to 0attr_accessor :players_hand
: Allows you to both read and write, as though you'd used bothattr_reader
andattr_writer
.Incidentally, all these do is write methods for you. If you wanted, you could do it manually like this: