0
I have this code. I cannot print the editor variable in the incrementNameTitle method, it is not being displayed. The result is as follows:
Publisher: In Flames - Collins!
class Editora
attr_accessor :nameEditora
def initialize(nameEditora)
@nameEditora = nameEditora
end
def incrementNameTitle
puts "Editora #{@nameEditora}: #{@title} - #{@author}!"
end
end
class Book < Editora
attr_accessor :title, :author
def initialize(title, author)
@title = title
@author = author
end
end
book = Book.new("Em Chamas", "Collins")
editora = Editora.new('LucasLivros')
puts book.incrementNameTitle
You urged the classes separately, so why use inheritance? A book is a publishing house?
– Woss
Um, I’m learning POO so I didn’t get your comment right. How can I access the variables title and Author in the Book class?
– Lucas
Inheritance should be used to create subtypes, or a specialization of an abstraction. In doing
Book
inherit fromEditora
You’re basically defining a book as a subtype of a publishing house. That doesn’t make sense. Perhaps what would make more sense there is composition - and who knows you can read more deeply about it.– Woss
Okay! Thank you very much!
– Lucas
How could I manage to access the name of the publisher next to the name and author of the book?
– Lucas