Object-oriented summary

Preface

I will continue to summarize the object-oriented outline. I also struggled to understand object-oriented programming, so I will write it in an easy-to-understand and concise manner for beginners. Masakari is fine, so I'd love to hear from you.

1. Object

An object means "object" and "object", and in programming it refers to a collection of data processing. In Ruby, all data such as arrays, hashes, numbers and strings are (mono) objects.

2. Object oriented

Object-oriented programming is the idea of creating and manipulating things (objects).

3. class

In object-oriented programming, a class is like a blueprint that groups data and processing together. Basically it can be used by creating an object based on the class

4. Property

The data that an object has is called a property (attribute).

5. method

The processing that an object has. In human terms, it is the process by which an object such as "run, walk, stop" takes some action.

def method name
What you want to do
end

If, for example, to define a hello method to call a "Hello"

def hello
  puts 'Hello'
end

hello #=>Hello

It becomes like.

6. instance

An instance is an object created from a class. Use the new method when creating an instance.

class human
  def initialize(name)
    @name = name
  end

  def hello
   puts "Hello"
  end
end

human = human.new("Tokugawa Ieyasu")

The initialize method defined here is a method that is automatically executed when an instance is created. The value "Ieyasu Tokugawa" is now set in the attribute name.

An instance called human created from the human class has a unique name of Ieyasu Tokugawa, and you can use a method called hello. You can create an object with this much information with just this one line.

7. Encapsulation

It means to hide the data and processing that an object has that does not need to be used directly from another object, and if it is used, it means to provide a processing created for external operation. The basic object-oriented concept that was mentioned in the racing car example earlier.

Encapsulation can be public or private. Methods described in public can be accessed from the outside, but if private is specified, an error will occur when calling.

For public

class human
  #public =>If nothing is specified, ruby defaults to public. Basically optional.
  def initialize(name)
    @name = name
  end

  def hello
   puts "Hello"
  end
end

human = human.new("Tokugawa Ieyasu")
puts human.name #=>Ieyasu Tokugawa is output

For private

class human
  private
  def initialize(name)
    @name = name
  end

  def hello
   puts "Hello"
  end
end

human = human.new("Tokugawa Ieyasu")
puts human.name #=>An error is output

8. Inheritance

To take over the function of a specific object and use it. When creating multiple similar objects, programming all the properties and methods one by one is very time-consuming, but inheritance allows you to implement the same functionality.

class Human
  def work
    puts 'I walked'
  end
end

#Inherit Human class
class IeyasuTokugawa < Human
  def unification
    puts "Unified the world"
  end
end

IeyasuTokugawa = IeyasuTokugawa.new
puts IeyasuTokugawa.unification #=>Unified the world
puts IeyasuTokugawa.work #=>I walked

When inheriting a class, you can use the methods defined in the parent class in addition to the methods defined in the class itself. In the previous example, the object of the "IeyasuTokugawa" class can execute the "work" method of the parent class in addition to the "unification" method.

You can see that not only the methods defined in the class but also the methods defined in the superclass (parent class) can be executed.

9. Polymorphism

What is Polymorphism? (Polymorphism) is one of the concepts and methods in object-oriented programming. Also called "diversity" in Japanese. It means that the same interface is provided for different classes.

The side using the class can call the method without being aware of the actual state of the class.

class Human

  def speak(voice='')
    "#{self.name}: #{voice}"
  end
end

#Inheritance
class NobunagaOda < Human
  def speak(voice='If it doesn't ring, kill it. Hototogisu')
    super
  end
end

class IeyasuTokugawa < Human
  def speak(voice='Float the ship well and overturn the ship well')
    super
  end
end

NobunagaOda = NobunagaOda.new
NobunagaOda.name = 'Oda Nobunaga'

IeyasuTokugawa = IeyasuTokugawa.new
IeyasuTokugawa.name = 'Tokugawa Ieyasu'

NobunagaOda.speak    #=>Oda Nobunaga:If it doesn't ring, kill it. Hototogisu
IeyasuTokugawa.speak     #=>Tokugawa Ieyasu:Float the ship well and overturn the ship well

It turns out that even with the same speak method, the behavior changes according to the definition in the class.

10. Advantages and disadvantages of object orientation

merit

1. Easy maintenance of the program

-Since each program can be considered as a small group, complicated programs are reduced. The impact of the repair will be reduced and maintenance will be easier.

2. Division of labor is possible

-The larger the system, the larger the program implementation. Object-oriented programming is parallel and can be implemented by a large number of people, increasing productivity. It is also possible to implement similar functions without affecting others.

3. Higher program quality

-Since every part is made into parts, it is easy to identify the problematic part. Because it is a cohesive program, the code is highly readable and intuitive.

Demerit

1. High difficulty and high education cost

・ Knowledge of baking blades is not useful for playing an active role in the development field. There are many abstract concepts, so to understand them properly Requires on-site experience

2. Design ability is tested

-Although convenient concepts such as inheritance and polymorphism can be used, design ability is indispensable in order to improve readability and quality such as influence, extensibility, and commonality with other code.

Finally

I'm still a beginner, so I don't have the technical skills. I think it's important to at least keep the basic concepts in mind when writing a program. I hope other beginners will remember it together.

Recommended Posts

Object-oriented summary
Summary
[Summary] Why make it object-oriented?
Object-oriented summary by beginners (Java)
Object-oriented programming
Object-oriented rock-paper-scissors
ransack summary
[Java] Object-oriented
Summary of object-oriented programming using Java
Docker basic summary
Java knowledge summary
Java Generics Summary
java.lang Summary 2 (String)
TDD training summary
Heroku command summary
Java related summary
My RxSwift Summary ②
iOS 14.4 IDFA Summary
Object-oriented FizzBuzz (Java)
[Java] Object-oriented summary_Part 1
My RxSwift Summary ①
Object-oriented programming terminology
[Java] Object-oriented syntax-Constructor
[Memo] docker summary
Object-oriented (Java) basics
Java 8 documentation summary
Anyway, Dagger2 summary
Docker command summary
Rails 6.0 Routing Summary
Eclipse refactoring summary
Object-oriented numerical calculation
[Java] Object-oriented summary_Part 2
Ruby syntax summary
iPLAss installation summary
Java 11 document summary
rails db: 〇〇 Summary
[Docker] Command summary
[Java] Object-oriented syntax-Package
Apache Apex Summary