class BookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price_in_cents Integer(price*100 + 0.5) end def price_in_cents=(cents) price = cents / 100.0 end
end book = BookInStock.new("isbn1", 33.80) puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}" book.price_in_cents = 1234 puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}"
看这段代码,能跑的,但是 price 的值没有赋进去,我知道因为 price = cents / 100.0 没有 写@price,但是这行能运行的,我想知道这个 price 的值到底赋到哪去了 price 这个变量在 price_in_cents 里没有出现过,那怎么可以用的么