[RUBY] I tried to summarize the logical way of thinking about object orientation.

About object orientation

Here is a brief summary of the terms used in the explanations below.
object=All data, things,

Object-orientation=In a word, "abstract concept" = make a thing with individual information from a collection of common information that each has

Method=Do something in programming. For example print("hello world")
"hello world"Is to say**object**.. The process to output this**print**Will be. That is, the print method)

class=The blueprint

instance=An entity created from a blueprint (class)

** Introduction **

Ruby and Python (there are other object-oriented languages) treat everything as an object. Each object returns a return value using its own unique properties and unique behaviors. For example, in the real world, each human being (a human being called Mr. A, a human being called Mr. B) is a separate object. One person has an object called "Mr. A". Being a human being, he has a unique name, gender, age, and hobbies, and can perform actions (methods) such as talking and walking.

** Why use objects? ** (I'll explain why.)

For example, if you explain with a car, the appearance of the car will not change, but there is a document compiled when you want to change the specifications of the car, so if you change only that document, you do not need to change the specifications of all cars one by one = Code description The amount can be made compact. = If you dig a little deeper, there is a merit of ** being strong against changes ** because it often happens that the code is rewritten due to specification changes at the engineer's site. (It's a little difficult to explain, but if you don't understand, please check the article that is a little easier to understand by yourself.)

** About classes and instances **

I would like to explain the relationship between a class and an instance by car. For example, you make a blueprint before you make a car, right? That is the ** class **. The vehicle (entity) actually created based on the blueprint is the ** instance **. I will give you a deeper explanation. A class is an object that collects common attributes (properties) and processes (methods). If you compare it with a game by using classes, you can easily divide the default settings (common information) into moving, running, and fighting. An instance is responsible for fine-tuning the class of abstract behaviors such as moving, running, and fighting. I call it an instance variable. (For example, in a fight, Mr. A can use magic and the power of the magic is 50 damage. Mr. B can use a hammer and the power of the hammer is 80 damage.)

** Class ** = Abstract (concept, common) ** Instance ** = Specific thing (embodiment, peculiar)

About class methods and instance methods (instance variables)

Earlier you learned the idea of classes and instances. Next, I'd like to add information to these classes and instances to learn how to handle them.

Instance method
python

①print(len("hello world")) #Output 11
②print(len("Good bye")) #Output 8

I checked the value of the string using the Len method of python. ① and ② use the same method, but the output values are different. These are called ** instance methods **.

Instance variables

The nature of an object is called an attribute, and its value is called an attribute value. For example, the color is an attribute and the red color is called the attribute value. This is called a ** instance variable **.

** Instance variables ** are variables that contain individual attribute values of an object and can be used for all operations of that object.


Class method

Class methods are the methods that a class can use. To explain it a little more clearly, it is ** processing that uses common information in the class **.

I'm sorry, I'm still studying class methods, so I'll stop here this time. I will correct it as soon as I understand it.

Recommended Posts

I tried to summarize the logical way of thinking about object orientation.
I tried to summarize the basic form of GPLVM
I tried to summarize the string operations of Python
[Machine learning] I tried to summarize the theory of Adaboost
I tried to summarize the umask command
I tried to summarize the graphical modeling.
[Linux] I tried to summarize the command of resource confirmation system
I tried to expand the size of the logical volume with LVM
I tried to summarize the frequently used implementation method of pytest-mock
I tried to touch the API of ebay
LeetCode I tried to summarize the simple ones
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
I didn't understand the Resize of TensorFlow so I tried to summarize it visually.
I tried to summarize how to use matplotlib of python
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to classify the voices of voice actors
I tried to summarize SparseMatrix
I tried to find the entropy of the image with python
I tried to find out the outline about Big Gorilla
[Horse Racing] I tried to quantify the strength of racehorses
I tried to get the location information of Odakyu Bus
I tried to find the average of the sequence with TensorFlow
I tried to summarize the code often used in Pandas
[Python] I tried to visualize the follow relationship of Twitter
I tried to summarize the commands often used in business
I tried to fight the Local Minimum of Goldstein-Price Function
I tried to summarize how to use the EPEL repository again
I tried to dig deeper about safety while calculating the stochastic finality of Proof of Work
I tried to organize about MCMC.
I tried to sort out the objects from the image of the steak set meal-① Object detection
I tried to move the ball
I tried to estimate the interval.
I tried to get the index of the list using the enumerate function
I tried to build the SD boot image of LicheePi Nano
I tried to summarize the commands used by beginner engineers today
I tried to improve the efficiency of daily work with Python
I tried to visualize the common condition of VTuber channel viewers
I tried to summarize the contents of each package saved by Python pip in one line
I tried to summarize Python exception handling
I tried the asynchronous server of Django 3.0
I tried to recognize the wake word
Python3 standard input I tried to summarize
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to summarize Ansible modules-Linux edition
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
I tried to summarize until I quit the bank and became an engineer
I tried to verify the best way to find a good marriage partner
I tried to get the batting results of Hachinai using image processing
I tried to visualize the age group and rate distribution of Atcoder
I tried transcribing the news of the example business integration to Amazon Transcribe
I tried to summarize the general flow up to service creation by self-education.
zoom I tried to quantify the degree of excitement of the story at the meeting
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I wanted to be careful about the behavior of Python's default arguments
I tried how to improve the accuracy of my own Neural Network
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]