Ruby:RPG清单系统
现在我终于了解了类继承在ruby中的工作原理,我正在为文本rpg设计一个清单系统。然而,有一些怪癖我无法理解 项目类别:Ruby:RPG清单系统,ruby,inheritance,system,Ruby,Inheritance,System,现在我终于了解了类继承在ruby中的工作原理,我正在为文本rpg设计一个清单系统。然而,有一些怪癖我无法理解 项目类别: class Item attr_accessor :name, :type, :attack, :armor, :wearloc, :weight, :price def initialize(name, type, attack, armor, wearloc, weight, price) @name = name @typ
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
但是,当我从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>
重型大刀(武器)
攻击5
佩带
体重15
价格2
米特里尔胸甲(盔甲)
装甲10
穿上身
体重15
价格5
金戒指(盔甲)
装甲3
戴指
重量2
价格20
#
#
#
为什么最后三行在那里?我不知道它是从哪里来的,也不知道如何修复它
更新:
使用实例变量而不是类变量为Itemclass 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
职业玩家
属性访问器:playername
def初始化(播放名称)
@playername=playername
结束
结束
职业目录 @ruby中的items方法返回方法中最后一条语句的值
在Inventory\Inventory
的情况下,最后一个语句是@@items。每个方法each
返回调用它的集合,因此返回值是@@items
数组
如果不希望该方法返回任何内容,可以在末尾加上nil:
def inventory
@@items.each do |item|
# code to print the item stats
end
nil
end
Ruby是这样的:p。这是一种很好的语言,尤其是当你学习了所有更精细的复杂问题之后。如果你在Ruby中使用@@class_变量
,你会过得很不愉快。我关于不使用类变量的评论的一些参考资料:和(不是公认的答案)@Ekult3k:这些都不应该相互继承。项目不是一种库存,而是包含在库存中。库存不是玩家的一种,它是由玩家携带的。因此,在播放器
中应该有一个@库存
,在库存
中应该有一个@项目
数组,通过方法或访问器公开。阅读关于“继承和组合”(谷歌有无数链接)。@oorahduc:正如我所说,阅读关于组合和继承的内容——你不必相信我的话。遗传是“A是B的一种”-狗是一种哺乳动物,ScreenWindow是一种ScreenWidget。。。组合(在实例变量中有另一个类的实例)表示“A有B”:父对象有一个子对象数组,子对象有一个父对象和一个父对象,Unicycle有一个轮子。@Ekult3k:你的观点是错误的。继承的全部意义在于共享相关对象的行为。若动物可以吃或死,哺乳动物可以吃或死,狗也可以吃或死。如果ScreenWidget可以隐藏自己,那么ScreenWindow也可以隐藏自己。如果项目继承自库存,则项目具有库存的所有行为,例如,如果您有库存#清除代码>和库存#丢弃随机物品!代码>,则您还有项#清除代码>和项目#删除随机项目!代码>,这毫无意义。
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
def inventory
@@items.each do |item|
# code to print the item stats
end
nil
end