--Writing person: Understand the basic grammar of programming languages, do not understand object orientation, during engineer training ――Before learning the object-oriented way of thinking that is necessary for learning Java, ** the aim is to get angry with the object-oriented world view and promote understanding. ** Also, my knowledge will be firmly established. Also.
--Object-oriented peripheral knowledge is required to understand Java session scope. ――Because I had avoided it until now, I thought about deepening my understanding of object-oriented programming.
Thorough commentary for beginners! What is object orientation? ・ It helped me to understand the technical terms around object orientation.
Object-oriented Iroha -Qiita ・ The explanation of abstraction was very helpful in thinking about programming and object-oriented background.
[Java] What is a constructor? Meaning of this \ () \ | The easiest introduction to Java ・ After understanding the rough knowledge (basic Java) and abstraction so far, I read this explanation and it became very familiar. Thanks to this explanation, various things have been connected.
[Object Oriented -Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF % E3% 83% 88% E6% 8C% 87% E5% 90% 91) -The six object-oriented summaries were helpful as rules for not derailing from object-oriented.
Introduction to Object-Oriented ・ Later, it seemed to be good for checking the degree of understanding, so a memorandum.
Clear Java Primer -It helped me understand the difference between an object and an instance.
But it is suddenly,
What do you think of when you think of an "apple"?
...
Speaking of "apples", "red, round and edible objects"
I think of you.
** I think I was able to understand it without specific information **.
This is called ** abstraction **.
As you can see from the example, ** abstractions are groups of similar features and given appropriate names for them. **
And I think the same thing can be said about programming.
In programming, there are many groups with similar characteristics.
The features are named variables, functions, and object-oriented.
If this is an apple, it can be easily explained as "a red, round and edible object", but in the case of programming, it takes time to understand because the abstracted object is very complicated and there are various patterns.
And object-orientation is one of those complex things.
However, I don't need to know various patterns now, so I would like to give a concrete example with one royal road pattern and try to abstract it to deepen my understanding of terms **.
~~ An object is an "object". ~~
~~ Object oriented ??? ~~
An object is generally an "object" In the world of programming, it is better to think of it as an "informed gathering" or an "abstracted object."
In the real world, most are made up of "objects".
An "object" is assumed to exist in space with a concrete shape.
However, it is difficult to understand if everything is an "object", so I decided to classify each feature and give a name to the "object".
** Yes, it's an abstraction. **
In this way, it is more irritating to adopt ** object-oriented ** by incorporating the idea ** "it is easy to handle if you think of it as an object like the real world", so I like that. understood.
And it is object-oriented programming that applied this idea to programming.
I think this is also from the idea ** "because it is easier to understand the programming world in the same way as the real world".
Since an object means an object in the real world, I thought it would be easier to understand by taking the real world as an example, so I will explain it with a ** minimum configuration using a car **.
** I made a car design document. Make a car based on the car design document. **
Simply put, that's it.
Furthermore, ** Make a "design document" that describes the specifications (color, function, weight, price) of the car, Manufactured based on "design document". **
That's all.
And ** when you have a car, drive it. **
↓
The main thing I do is ** "Create a design document to create an object, create an object, and move the object." **
(I was most hungry for this way of understanding.)
This time, I would like to take the process of actually making a red car as an example.
In the world of programming, the code is loaded in order from the top.
For example, when the following sentence (the sentence is written instead of the code for easy understanding), it is read in order from the top.
** 1. Name the design document "Car". 2. Write information about the car "color is red" and "name is A" in the contents of the design document. 3. In the contents of the design document, write the actions related to the car such as "can make a call" and "can stop". 4. Make a "car" based on the "car design document". **
Then, in terms of programming, a "red car called A" will be completed.
In this way, ** Object-oriented programming is the idea of creating a "design document", writing "what kind of thing" and "what can be done" in it, and treating it in the same way as a real thing. **
However, there may be times when you want the color of the car to be "blue".
In that case, rewrite part 2 from "red" to "blue".
Then, "blue car called A" is completed.
This is how easy it is to make changes in programming.
In the real world, if you try to do this, you have to take the process of collecting the car, painting it in blue from above, and returning it (and it is very difficult if there are many cars, it is a so-called recall. …).
However, in the world of programming, all you have to do is rewrite the contents of the "car design document".
Somehow, I think I've learned how to write object-oriented.
It's a rough idea, but if you can understand the meaning so far
It's almost as if I knew about it.
** These terms are just names for each and every event in the previous example. ** (Actually it's more complicated, but ... it's easy for me to understand.)
-** Object-oriented → The idea of treating objects in the same way as real-world objects ** -** Object → Generic name for objects ** (car) -** Class → Name of design document ** (Car design document) -** Field → Object information ** (color is red, name is A) -** Method → Object movement ** (Can make or stop) -** Instance → Object made based on the design document ** (Car made based on the car design document) -** Instantiation → Creating an object based on the design document ** (Making a car based on the car design document)
It's difficult to put it in terms, but what I'm doing is ** "Create a design document to create an object, create an object, and move the object." **.
A simple diagram of this relationship is as follows.
An object is a general term for dynamically generated entities. Instance is a general term for dynamically generated entities of class type.
It looks like this in the figure.
When used in the context of just an entity, both objects and instances seem to have the same meaning.
Car.java
class Car {
}
[Program description] -The type is a class type and the class name "Car" is declared. -Class types are classified as reference types.
Car.java
class Car {
String carname = "A"; //Car name
String color = "red"; //Car color
}
[Program description] -String type car name that represents the name of the car -String type color that represents the color of the car
Car.java
class Car {
String carname = "A"; //Car name
String color = "red"; //Car color
void start() { //Car outgoing operation
System.out.println("Take off");
}
void stop() { //Car stop operation
System.out.println("Stop");
}
}
[Program description] -Represents the outgoing operation of the car. Return type start method that does not return a value ・ Indicates the stop operation of the car. Return type stop method that does not return a value
CarTest.java
class CarTest{
public static void main(String[] args){
Car car = new Car();
}
}
[Program description] -The type is a class type and the class name "CarTest" is declared. -Instantiate the Car class with the new operator and assign an object to the Car type variable car (class type variable). → ** It looks like you are substituting the created object, but in fact, the variable Car just refers to (sees) the object called Car (the access method is part 2) **.
It looks like this in the figure.
Although it is a rough sketch, I made a design document for making an object and summarized it while referring to various things up to the point of making an object.
Continue to [Java] Object-Oriented Summary \ _Part 2 -Qiita.
Recommended Posts