【发布时间】:2011-09-16 04:30:47
【问题描述】:
我一直在为一堂课学习 Ruby,并且一直在编写一个示例游戏。这是我的一堂课:
class Player
def askIfTake
puts("Would you like to take a card? > ")
input = gets.chomp
input.downcase!
if input == "y" or input == "yes"
return 1
elsif input == "n" or input == "no"
return 0
else
puts("Invalid input. Please type y or n.")
return askIfTake
end
end
end
然后我有另一个课:
class PlayerAI < Player
def initialize
super
end
def askIfTake
puts("this is an AI")
return rand(2)
end
end
问题是,当我创建 PlayerAI 的实例并尝试从该实例调用 askIfTake 时,它会调用 Player 类中声明的方法。为什么会这样?
【问题讨论】: