我正在学习红宝石封装的概念。
下面是示例代码,笔记本电脑从机器继承,我已经吃了一个受保护的方法(机器类),但不能通过笔记本电脑的实例访问。
另外,我无法访问笔记本电脑的名为description的受保护方法。
class Machine
attr_accessor :name,:cost generated
protected
def eat
puts "machine don't eat"
end
end
class Laptop < Machine
private
def ram
return "4gb"
end
private
def core
return "i3"
end
protected
def description
puts "The laptop has #{ram} ram and it has #{core} core"
end
end
laptop=Laptop.new
laptop.name="hp_notebook_15_n205tx"
laptop.cost =44000
puts "laptop is a machine, & #{laptop.eat} " #doesn't work
puts "#{laptop.name} costs #{laptop.cost}"
puts "#{laptop.description}" #doesn't work
下面是我得到的错误:
`<top (required)>': protected method `eat' called for #<Laptop:0x2ed3b68 @name="hp_notebook_15_n205tx", @cost=44000> (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'