“Object oriented” in the eyes of Xiaobai



《Head First Java》It’s the first book I’ve read since I studied computer. It’s also the most interesting programming language course I’ve ever seen.
Different from other books, this book, or the whole《Head First》The biggest difference of series is that they don’t write like a tutorial at all, but like a novel… So that readers can understand many concepts that teachers can’t “fill in ducks” in a pleasant atmosphere.

This article is an object-oriented in Xiaobai’s eyes.

As the undergraduate students of an unknown University, they are quite aware of the computer learning in the University,
For non computer major students, our only computer course is VC programming, which is C + + on the surface, but actuallyCBecause it does not involve anyClasses and objectsThat’s allProcess oriented。 The learning content is just input and output, data type, simple cycle and judgment… The most profound thing most students remember about the learning effect is:

using namespace std;
int main(){
    return 0;

And then write something… I don’t know
That’s the level!

So, like many college students, they don’t know what object-oriented is before learning Java, and the thinking in their mind is also process oriented.

1、 Process oriented

First, what is process oriented?
process”At the thought of these two words, the first reaction in my brain is the processing plant and production line, which can also be understood as a function in mathematics.

“What does it take to make a cup of coffee?”
It needs water and coffee powder as raw materials. First, heat the water, and then pour it into the coffee powder. Then we can draw such a picture.

“Process” is a pipeline. When designing a process oriented program, we only need to consider,From input to output, how to implement it and what steps it has gone through. Then, the implementation mode is divided into specific steps, which are executed in sequence when the program is runningJust fine. So process oriented can be abstracted into the following figure:

In process oriented, all data is usedvariableTo store, all the steps are throughfunctionTo achieve.
When facing the process, all we think about is the process, that is, every process in the factory.

2、 On the stage of object-oriented

So what’s the difference between object orientation?

Object-oriented thinking may be closer to real life.
When I try to understand the object by myself, what my brain shows is not a processing factory, but a lively park. There are many people and all kinds of small animals in the park. Children are playing, bees are gathering honey in the flowers, and there is a writer sitting on a stone bench who is going to turn on the computer to write a manuscript

In this picture, a child, a bee, a flower, a writer, and the computer in his hand, everything isobject
And between these objects, in some way, there is interaction.
So, the biggest difference between object-oriented and process oriented is that object-oriented is no longer concernedWhat steps are needed to complete something, but pay more attention toHow do things relate to each other

Object contains two important contents: one isObject properties, the other isObject method
Take the notebook in the hands of the author just mentioned as an example: a notebook must have a state — power on or power off, so its state is an attribute; the notebook also has memory size, hard disk capacity, CPU model, etc., which are the attributes of this notebook.

In short:The “property” of an object is the “variable” mentioned in process oriented

and“Method”What is it? In real life, one way is to realize a function. For example, the “power on” method of the notebook is to turn the status of the notebook from off to on. Then “power on” is one way of the notebook. The notebook can also open the document and display the content of the document. Then “print document information” is another way of the notebook Method, and this method has an output value, which is a string of document information.
So how to explain the “method” of the object? ——It can be understood as the process oriented“function”。

But this is not rigorous or completely correct, because variables are only used to store data objectively, while attributes can also store data, but they areA feature of an objectVariables can exist without objects, but properties cannotIf there is no object, the property will lose its meaning (if there is no computer, can you imagine what kind of state “power on” is?).
Similarly, a function represents a process of data processing, while a method represents a way in which this object contacts other objects,Methods cannot exist without objectsIt’s hard for someone who has never seen a computer to turn it off.

Three, class

We continue to take notebook computers as an example. Xiaoqiang, Xiaoda and Xiaoxuan all have their own notebook computers, and these computers have common characteristics, so we can say that they belong to the same “category”.

What is “class”?
What are the classes in life? In popular language, it is a group of things with common characteristics. So classes are templates and blueprints of a group of objects with common properties and methods. If several objects belong to the same class, this class contains the properties and methods shared by these objects. In addition, you can create new objects from this class. The newly created objects also have the characteristics of this class.

Perhaps when we first touch classes and objects, we don’t feel the use of it. We continue to say, if we need to use variables to represent the information of Xiaoqiang, Xiaoda and Xiaoxuan’s computers, what should we do?

Suppose that each notebook has three information: disk capacity, memory size, and on-off status.

If in the process oriented thinking, it is like this:

//Xiaoqiang's notebook
int XiaoQiang_Disk;
int XiaoQiang_Memory;
bool XiaoQiang_Status;

//Xiaoda's notebook
int XiaoDa_Disk;
int XiaoDa_Memory;
bool XiaoDa_Status;

//Xiaoxuan's notebook
int XiaoXuan_Disk;
int XiaoXuan_Memory;
bool XiaoXurn_Status;

3 * 3 = 9 variables need to be defined.

What if it’s object-oriented?

//Define a notebook class
class laptop{
    int disk;
    int memory;
    bool status;

//Create three objects based on this class
laptop XiaoQiang = new laptop();
laptop XiaoDa = new laptop();
laptop XiaoXuan = new laptop();

3 + 3 = 6 variables need to be defined.

Maybe it doesn’t matter. Just write a few fewer variables? But imagine if the number of notebooks to represent is increased to 10000?
In the process oriented world, 10000 * 3 = 30000 variables need to be defined, while using object-oriented thinking, only 10000 + 3 = 10003 variables need to be defined.

To be exact, if an object is created through a class, the object will copy all the non-static properties and non-static methods of the class. As long as the object is created, it is equivalent to creating all the properties of the class once. So, one of the simplest benefits of OO is thatChange the information that can only be expressed by creating new exponential variables into the information that can only be expressed by creating new linear level variablesNow.

Four, inheritance

Another benefit of object orientation is inheritance.
“Inheritance” is a very vivid word, which makes people instantly think that in life, parents give their property to their children. The action of children accepting property isinheritInheritance can also be said that the “next generation” has acquired something from the “previous generation”. What is “acquisition”? –Properties and methods

The inherited class is calledParent classThe class that performs the inheritance is calledSubclass(the class that asks for something). Inheriting this action can be understood as “taking a class as a template and creating a new class”.

In other words, computers can be divided into many kinds: PC, notebook, server… They are all computers, with many common features, all of them have disks, memory, CPU… So we can establish a “computer” class, which represents the templates of all computers. However, they have their own characteristics. All laptops have batteries, all servers have ECC verification memory, and all PCs have monitors… So, these three classes can be established respectively based on the characteristics of the three classes, all inherited from the “computer” class, so that they both retain the public properties and have their own characteristics.

5、 Polymorphism and interface

There’s more to it – polymorphism and interfaces… Because of the limited capacity at the moment, I’ll keep it for the next time.

Six, summary

In my opinion, object-oriented is closer to real life than process oriented. Because an object can be a real object in life, it is only necessary to abstract the common attributes of these objects, that is, a class. Object oriented thinking is easier to understand and has many advantages (inheritance, interface, polymorphism…).

Last year, when I first learned computer, I was“Process oriented“And”Object-oriented”These two words are confusing. The teacher’s explanations are ambiguous, which makes me not understand what the teacher is talking about at all. Later, I checked some posts and found that what they said is not very clear… Until now, I have studied PHP and Java, and really done a project, I have a deeper understanding of these two words. If there is any wrong expression or inaccessibility of the above concepts, please point out.