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)
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.
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.)
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)
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 **.
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 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