July 11, 2020 Progate Lv.175 RubyⅤ
You can create a class from scratch, but you can use an existing class and create a new class based on that class. By doing so, you can put together the common parts and write the code efficiently.
Creating a new class based on a certain class is called inheritance. You can define a new class by inheriting another class with class new class name <original class name
. The new class is called the "child class" and the original class is called the "parent class".
The instance method of the parent class is inherited.
food.rb
require "./menu"
class Food < Menu
end
When an instance of a child class inherits, the child class inherits the instance method of the parent class.
index.rb
food1=Food.new
puts food1.name
puts food2.info
menu.rb
class Menu
attr_accessor :name
#processing
def info
return "#{self.name} #{self.price}Circle"
end
end
Use ʻattr_accessor` as before to add instance variables to child classes.
menu.rb
class Menu
attr_accessor :name
attr_accessor :price
end
Because the Food class inherits from the Menu class. It has three instance variables "name, price, calorie".
food.rb
class Food < Menu
#Add calorie to Food class
attr_accessor :calorie
end
Instance methods can be added in the same way.
You can override a method by defining a method in the child class that has the same name as the parent method. This is called overriding.
index.rb(Call the method)
food1~Food.new(...)
food1.calorie=700
puts food1.info
menu.rb(Parent class)
class Menu
#processing
def info
#processing
end
end
food.rb(Child class)
class Food < Menu
#processing
#Method overwrite (overwritten method is called)
def info
#processing
end
end
The instance of the child class preferentially calls the method defined in the child class. Therefore, if the child class and the parent class have methods with the same name, the contents of the method of the child class are overwritten. You can also override it with the initialize method.
super
You can call a method with the same name in the parent class by setting super
in the overridden method. Since super calls a method, it is necessary to pass an argument to super according to the method of the parent class.
menu.rb
class Menu
attr_accessor :name
attr_accessor :price
def initialize(name:, price:)
self.name=name
self.price=price
end
end
food.js
class Food < Menu
attr_accessor :calorie
def initialize(name:, price:, calorie:)
super(name: name, price: price)
self.calorie=calorie
end
end
A class that handles dates. Date class is a class already prepared by Ruby, and you can use it without defining a class by reading date with require
. Classes already prepared have a different way of writing require than others </ font>
index.rb
# "/date"is not!
reruire "date"
You can create an instance with Date.new
. You can create an instance of today's date with Date.today
.
index.rb
require "date"
#Create Date method by passing "year / month / day" as an argument
date1=Date.new(2020,7,8)
puts date
console
2020-07-08
There are many instance methods. It is the sunday?
Method that passes whether it is Sunday or not as a boolean value.
index.rb
require "date"
date1=date1.new(2020,7,11)
puts date1.sunday?
console
false
A method to call on a class. The place of today
of Date.today
. Class methods can be defined by using def class name.method name
. Unlike the instance method, write the class name before the method name.
Call the class method with class name.method name
.
menu.rb
class Menu
#processing
#Returns a boolean value indicating whether today's date is Sunday
def Menu.is_discount_day?
#Date instance with today's date information
today=Date.today
return today.sunday
end
end
puts Menu.is_discount_day?
Call a class method inside an instance method
menu.rb
class Menu
#processing
def get_total_price #Instance method
if Menu.discount_day? #Calling a class method
#processing
end
end
end
Recommended Posts