Ruby 继承初始化参数
我想知道如何正确初始化子类“Computer”。我希望它继承游戏类中initialize中的属性,但#start是一个方法。在这种情况下,我也不确定如何处理initialize方法中的参数。有人知道一个优雅的方式来重新措辞吗?谢谢Ruby 继承初始化参数,ruby,inheritance,initialization,subclass,super,Ruby,Inheritance,Initialization,Subclass,Super,我想知道如何正确初始化子类“Computer”。我希望它继承游戏类中initialize中的属性,但#start是一个方法。在这种情况下,我也不确定如何处理initialize方法中的参数。有人知道一个优雅的方式来重新措辞吗?谢谢 class Game attr_reader :input, :clues def initialize colors = %w(R O Y G I V) code = [] all = ''
class Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
start
end
def start
...
end
def ask_input
...
end
class Computer < Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
ask_input
computer_turn
end
.....
end
类游戏
属性读取器:输入,:线索
def初始化
颜色=%w(R O Y G I V)
代码=[]
全部=“”
计数=0
开始
结束
def启动
...
结束
def ask_输入
...
结束
课堂电脑游戏
属性读取器:输入,:线索
def初始化
颜色=%w(R O Y G I V)
代码=[]
全部=“”
计数=0
询问您的输入
电脑转向
结束
.....
结束
我希望它继承游戏类中initialize中的属性,但#start除外,它是一个方法
所有属性和方法都将被继承。您在以下方面做得很正确:
class Computer < Game
计算机初始化后,它的输入
等于:foo
class Computer < Game
def initialize input
@input = input
...
由于您不需要方法
start
,只需将其从Game
类中删除,这样它就不会出现在您的子类中。比如:
class Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
(Insert what start does here)
end
def ask_input
...
end
然后,只需使用以下命令覆盖计算机
子类的初始化:
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
(insert other functionalities)
end
您还可以消除冗余的attr\u读取器
,因为它是从Game
我也不确定在这种情况下如何处理initialize方法中的参数
你只是
- 在子类的
中添加初始值设定项
,以调用其超类的super
李>初始值设定项
- 当然,所有实例变量在开始时都应该有
字符,以使它们在所有实例方法中都可用@
- 同时从
类中删除计算机
,因为它将从属性读取器
类继承游戏
- 最后,为了避免调用
类的游戏
方法,我认为您只需要在#start
类中重写它计算机
class Game
attr_reader :input, :clues
def initialize
@colors = %w(R O Y G I V)
@code = []
@all = ''
@count = 0
start
end
def ask_input
# sample value for @input
@input = 'sample input'
end
def start
puts "start"
end
end
class Computer < Game
#attr_reader :input, :clues
def initialize
super
ask_input
computer_turn
end
def start
# Do nothing
end
def computer_turn
puts "computer_turn"
p @colors
end
end
comp = Computer.new
# The string "start" is not puts here because Game#start is not called
=> computer_turn
=> ["R", "O", "Y", "G", "I", "V"]
comp.input
=> "sample input"
类游戏
属性读取器:输入,:线索
def初始化
@颜色=%w(R O Y G I V)
@代码=[]
@全部=“”
@计数=0
开始
结束
def ask_输入
#@input的样本值
@输入='样本输入'
结束
def启动
“开始”
结束
结束
课堂电脑游戏
#属性读取器:输入,:线索
def初始化
超级的
询问您的输入
电脑转向
结束
def启动
#无所事事
结束
转向
把“电脑转”
p@colors
结束
结束
comp=Computer.new
#字符串“start”不放在这里,因为没有调用Game#start
=>计算机\u转
=>[“R”、“O”、“Y”、“G”、“I”、“V”]
复合输入
=>“样本输入”
单向-甚至不要在初始化中执行此操作,而是定义自定义设置器,例如定义颜色@颜色| |=%w(R O Y G I V);结束游戏
,该游戏将在计算机
中提供。虽然我通常建议将变量重新构造为自定义setter方法,以实现更干净的继承,但请参阅以获取问题的答案。我不希望计算机继承#开始。我希望它以ask_输入开始。它不能像上面写的那样工作。您可以将start
标记为private,这样它就不会被继承。
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
(insert other functionalities)
end
class Game
attr_reader :input, :clues
def initialize
@colors = %w(R O Y G I V)
@code = []
@all = ''
@count = 0
start
end
def ask_input
# sample value for @input
@input = 'sample input'
end
def start
puts "start"
end
end
class Computer < Game
#attr_reader :input, :clues
def initialize
super
ask_input
computer_turn
end
def start
# Do nothing
end
def computer_turn
puts "computer_turn"
p @colors
end
end
comp = Computer.new
# The string "start" is not puts here because Game#start is not called
=> computer_turn
=> ["R", "O", "Y", "G", "I", "V"]
comp.input
=> "sample input"