现在我终于找到了类继承如何在ruby中工作,我正在为文本RPG设计一个库存系统。然而,有一些我无法弄清楚的怪癖。
项目类:
class Item
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end
库存类:
class Inventory
attr_accessor :items
def initialize
@@items = []
end
def add_item(item)
@@items << item
end
attr_accessor :inventory
def inventory
@@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
end
end
玩家类:
class Player < Inventory
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end
实例化库存,创建一些项目,将它们添加到库存中都可以。
inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)
但是,当我从Player类调用inventory方法时,我得到了所需的输出,加上3个#item对象,如下所示:
a heavy broadsword (weapon)
Attack 5
Wear wield
Weight 15
Price 2
a mithril breastplate (armor)
Armor 10
Wear torso
Weight 15
Price 5
a gold ring (armor)
Armor 3
Wear finger
Weight 2
Price 20
#<Item:0x007fae1b8b11d8>
#<Item:0x007fae1b8b1138>
#<Item:0x007fae1b8b1098>
为什么最后三行在那里?我无法弄清楚它的来源或解决方法。
更新
重新修改项目&lt;库存&lt;使用实例变量而不是类变量的播放器继承。
class Player
attr_accessor :playername
def initialize(playername)
@playername = playername
end
end
class Inventory < Player
attr_accessor :items
def initialize
@items = []
end
def add_item(item)
@items << item
end
attr_accessor :inventory
def inventory
@items.each do |item|
puts "#{item.name} (#{item.type})"
if item.attack != nil ; puts " %-20s %00d" % ['Attack', item.attack] ; end
if item.armor != nil ; puts " %-20s %00d" % ['Armor', item.armor] ; end
if item.wearloc != nil ; puts " %-20s %00s" % ['Wear', item.wearloc] ; end
if item.weight != nil ; puts " %-20s %00d" % ['Weight', item.weight] ; end
if item.price != nil ; puts " %-20s %00d" % ['Price', item.price] ; end
end
nil
end
end
class Item < Inventory
attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price
def initialize(name, type, attack, armor, wearloc, weight, price)
@name = name
@type = type
@attack = attack
@armor = armor
@wearloc = wearloc
@weight = weight
@price = price
end
end
inv = Inventory.new
broadsword = Item.new('a heavy broadsword', 'weapon', 5, nil, 'wield', 15, 2)
breastplate = Item.new('a mithril breastplate', 'armor', nil, 10, 'torso', 15, 5)
ring = Item.new('a gold ring', 'armor', nil, 3, 'finger', 2, 20)
inv.add_item(broadsword)
inv.add_item(breastplate)
inv.add_item(ring)
player = Player.new('Chris')
# puts player.inventory
puts inv.inventory
答案 0 :(得分:2)
ruby中的方法返回方法中最后一个语句的值。
在Inventory#inventory
的情况下,最后一个语句是@@items.each
方法。 each
返回调用它的集合,因此返回值为@@items
数组。
如果你不想让方法返回任何东西,你可以在最后加一个零:
def inventory
@@items.each do |item|
# code to print the item stats
end
nil
end