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.
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.
Object-oriented programming is the idea of creating and manipulating things (objects).
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
The data that an object has is called a property (attribute).
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.
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.
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.
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
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
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.
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.
-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.
-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.
-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.
・ 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
-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.
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