iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 13
0
影片教學

懶人寫寫 BOT系列 第 13

Day 13

嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 解題增加內容
https://www.hackerrank.com/domains/tutorials/30-days-of-code

Day 13: Abstract Classes

  • You cannot instantiate an abstract class.
  • You must override this method in your implementing class.
  • chomp your input incase \n

## This is a **simulation** of an abstract class provided at user request. ##
class Book
  attr_accessor :title
	attr_accessor :author

	def initialize(title, author)
		raise 'You cannot instantiate an abstract class.'
	end

	def display
		raise 'You must override this method in your implementing class.'
	end
end

class MyBook < Book
    attr_accessor :price

    #   Class Constructor
    #
    #   Parameters:
    #   title - The book's title.
    #   author - The book's author.
    #   price - The book's price.
    #
    # Write your constructor here
    def initialize(title, author, price)
      # super(title, author)
      @title = title
      @author = author
      @price = price
    end

    #   Function Name: display
    #   Print the title, author, and price in the specified format.
    #
    # Write your function here
    def display
      puts "Title: #{@title.chomp}\nAuthor: #{@author.chomp}\nPrice: #{@price.chomp}"
    end

    title = gets
    author = gets
    price = gets

    new_novel = MyBook.new(title, author, price)
    new_novel.display
end

上一篇
Day 12
下一篇
Day 14
系列文
懶人寫寫 BOT30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言