About class inheritance.

No. title
1 Markdown memorandum
2 Current article
3 Second post

How to write class inheritance

class Apple < Fruit
      #Child class#Parent class
end

In the above case, Fruit is the parent class and Apple is the child class.

class Fruit

  def initialize(fruit_name, fruit_color)#(First argument,Second argument)Formal argument
    #The initialize method is processed at the same time as the new method is executed when the instance is created. No new description is needed for the call.
    @fruit_name = fruit_name
    #@fruit_name is what is called an instance variable.
    @fruit_color = fruit_color
  end

  def name
    puts "this is#{@fruit_name}is."#{value}でvalueの展開を行なっています。
  end

  # def color
  #   puts "#{@fruit_color}The color."
  # end

end

class Apple < Fruit
     #Child class#Parent class

  def name
    puts "This is delicious#{@fruit_name}is." 
    #It can handle instance methods and instance variables defined in the parent class. It is a feature of class inheritance.
    #It is also possible to overwrite the instance method by using the one with the same name as the instance method of the parent class in the child class.
  end
   
  def color
    puts "beautiful#{@fruit_color}The color."
    #You can also handle instance methods that are only child classes.
  end

end


apple = Apple.new("Apple", "Red")#The actual argument, the actual argument and the formal argument must match the number.
apple.name #Generated instance name.You are calling an instance method with an instance method name.
apple.color

The output is ** This is a delicious apple. **** It is a beautiful red color. ** ** It will be.

Recommended Posts

About class inheritance.
About inheritance
[Java] Class inheritance
About Ruby inheritance
About Java class
About java inheritance
About the StringBuilder class
About class division (Java)
[Swing] About JFrame class
About inheritance (Java Silver)
About Java String class
About encapsulation and inheritance
About sorting java.util.Arrays class
About java abstract class
Find out about class methods
Inheritance
Write class inheritance in Ruby
Inheritance
About the File :: Stat class
About Java class loader types
About =
[Java] What is class inheritance?
About Java class variables class methods
A murmur about the utility class
About object-oriented inheritance and about yield Ruby
About method.invoke
About Kotlin
About attr_accessor
About Hinemos
About params
About Docker
About Rails 6
About form_for
11th class: Class
About Spring ③
[Java] Inheritance
About enum
About polymorphism
About Optional
About hashes
About JitPack
About Dockerfile
Java inheritance
Anonymous class (anonymous class)
[Java] Inheritance and structure of HttpServlet class
About this ()
About devise
About encapsulation
Java inheritance
About Docker
About JAVA_HOME
About active_hash
About static
Class method
About exceptions
ObjectMapper class
Advanced inheritance
About scope
ArrayList class
java (inheritance)
[Maven] About Maven