Object-Oriented Programming (Basic) 1: Understanding the Essence of Object-Oriented Thought

Table of contents

1. Overview of object-oriented programming (understanding)

1.1 Ideas of program design

1. Process-Oriented Programming (POP for short)

2. Object Oriented Programming (OOP)

1.2 Consider how to design programs from practical problems

Thinking 1: How to drive?

Thinking 2: How to build a car?

 Analogy Example 2: Human puts an elephant in a refrigerator

process oriented

object oriented


1. Overview of object-oriented programming (understanding)

1.1 Ideas of program design

Object-oriented is a kind of programming style and development paradigm in software development. In addition 面向对象, there are 面向过程, 指令式编程and 函数式编程. Among all programming paradigms, the ones we are most exposed to are process-oriented and object-oriented.

类比:史书类型

纪传体:以人物传记为中心,“本纪”叙述帝王,“世家”记叙王侯封国和特殊人物,“列传”记叙民间人物。
编年体:按年、月、日顺序编写。
国别体:是一部分国记事的历史散文,分载多国历史。

In the early days, there was process-oriented thinking. With the expansion of software scale and the increase of problem complexity, process-oriented thinking became more and more 弊端obvious, and object-oriented thinking appeared and became the current mainstream method.

1. Process-Oriented Programming , referred to asPOP

  • The focus of attention is 过程: the process is the steps to manipulate the data. If the implementation code of a certain procedure is repeated, then this procedure can be extracted as one 函数. This can greatly simplify redundant code and facilitate maintenance.
  • Typical language: C language
  • Code structure: 函数Think organizational unit.
  • Is a " 执行者思维", suitable for solving simple problems. Poor scalability and difficult post-maintenance.

2. Object Oriented Programming , referred to asOOP

  • The focus of attention is : in the process of computer programming, refer to the real things, abstract the attribute characteristics and behavior characteristics of things, and express them with classes.
  • Typical languages: Java, C#, C++, Python, Ruby, PHP, etc.
  • Code structure: Think organizational unit. Every thing has its own 属性sum 行为/功能.
  • Is a " 设计者思维", suitable for solving complex problems. The code has strong scalability and high maintainability.

1.2 Consider how to design programs from practical problems

Thinking 1: How to drive?

When thinking about problems with process-oriented thinking, we first think about " 怎么按步骤实现?" and map the steps into methods, step by step, and finally complete. This is suitable for situations 简单任务where you don't need it 过多协作. For how to drive, you can list the steps:

 Process-oriented is suitable for simple, non-cooperative transactions, focusing on how to execute.

Thinking 2: How to build a car?

Building a car is too complicated, and it needs 很多协作to be done. At this time, what we are thinking about is " 车怎么设计?", not "how to build a car according to specific steps". This is the change in the way of thinking, the former is object-oriented thinking. Therefore, the object-oriented (Oriented-Object) thinking is more in line with the human thinking mode.

Think about "how to design a car" with object-oriented thinking:

Naturally, we will start thinking about "what is a car made of". It is found that the car consists of the following structure:

We find a tire factory to complete the steps of manufacturing tires, and an engine factory to complete the steps of manufacturing engines, ... In this way, everyone can manufacture cars at the same time, and finally assemble them, which greatly improves efficiency. However, when it comes to an assembly line operation in a tire factory, there are still steps, and process-oriented thinking is still inseparable!

Therefore, object-oriented can help us grasp and analyze the whole system from a macro perspective.  However, when it comes to the micro-operations of the implementation part (that is, each method), it still needs a process-oriented approach to deal with.

注意:

我们千万不要把面向过程和面向对象对立起来。他们是相辅相成的。面向对象离不开面向过程!

当需求单一,或者简单时,我们一步步去操作没问题,并且效率也挺高。

可随着需求的更改,功能的增多,发现需要面对每一个步骤很麻烦了,这时就开始思索,**能不能把这些步骤和功能进行封装,封装时根据不同的功能,进行不同的封装,功能类似的封装在一起。**这样结构就清晰了很多。用的时候,找到对应的类就可以了。这就是面向对象的思想。

 Analogy Example 2: Human puts an elephant in a refrigerator

process oriented

1.打开冰箱

2.把大象装进冰箱

3.把冰箱门关住

object oriented

人{
    打开(冰箱){
		冰箱.开门();	
    }
    操作(大象){
             大象.进入(冰箱);
    }
    关闭(冰箱){   
          冰箱.关门();     
    }
}

冰箱{
     开门(){ }  
     关门(){ }
}

大象{
     进入(冰箱){  }
}

        

Guess you like

Origin blog.csdn.net/swx595182208/article/details/129947170