Object-Oriented Programming appreciated python

What is Object-Oriented Programming?

OOP (Object-oriented Programming, referred to as OOP), "object-oriented" programming is an idea, it "process-oriented" programming ideas are very different, with different programming ideas in solving different types of problems can greatly enhance code efficiency, two ideas are not good or bad.

Process-oriented : to analyze the steps required to solve the problem, then use the function to implement these steps, step by step, when used in a call to a turn on it.

Object-oriented : to be a problem affairs broken down into individual objects, the object was not intended to establish a complete step, but a thing to Miao Xu behavior throughout the problem-solving steps in.

for example:
Example 1: The dog caged implemented
process for:
(1) open the cage
(2) put into the dog
(3) off the cage
object-oriented:
create an object, this object can be completed "dog put into the cage." behavior, call the object to complete this series of acts
from process-oriented - object-oriented programmers realized the transition from performer to director of

Example 2: backgammon game implemented
process for:
(1) start the game - (2) preceding sunspots - (3) drawing board - (4) determining winners and losers - (5) placed albino - (6) drawing board - - (7) determining winners and losers - (8) returns to step (2) - (9) the output of
the object-oriented:
the entire backgammon game system may be divided into three parts:
(1) player system: both black and white, the same pattern of behavior
( 2) system board: the board is responsible for mapping real-time
(3) regulation system: judge, foul, fail, win, draw and other real-time chess game of
first-class objects (Enthusiast system) is responsible for receiving user input and passes the results to the first input Object type II (system board), chess game to inform its variations; second class object (system board) is responsible for drawing board, and the dynamic variation of the output chess game on the screen, while using the third class object (system rules) on the game situation determination.
As can be seen, the process is for specific steps to solve the problem, the problem is to divide a functional object oriented.

Example 3: Implementing the following output:
Guo, 20 years old, walking to travel
Guo, 30 years old, raising a dog
Guo, 40 years old, to watch cartoons
old Liu, 20 years old, traveling on foot to
the old Liu, 30 years old, still not married
Liu, 40 years old, to watch cartoons
process-oriented:
Here Insert Picture Description
Object-oriented:
Here Insert Picture Description
output:
Here Insert Picture Description
If you want to enter the age of 20 --90-year-old state of all life and Liu Guo it? Using process-oriented, it is necessary in the implementation of the function input parameters {many times ( "Kuo", 20), ( "Lao", 30)}, but the use of object-oriented, it will only create the object required parameters encapsulated within the object, when using the parameter again,Go directly to the object by calling the current selfTo

Creating classes and objects

面向对象的编程方式需要用“类”和“对象”来实现。
简单来说,类就是一个模板,模板里可以包含多个函数,函数里实现一些功能;对象则是根据模板创建的实例,通过实例对象可以执行类中的函数。
Here Insert Picture Description

什么是__init__()?

称其为构造方法或构造函数
首先要明确:类就是一种对象类型, 跟数值、字符串、列表等等类型一样。比如这里构建的类名字叫做Person,那么就是我们要试图建立一种对象类型,这种类型被称之为Person,就如同有一种对象类型是list一样。
在构建Person类的时候,首先要做的就是对这个类进行初始化,也就是要说明这种类型的基本结构,一旦这个类型的对象被调用了,第一件事情就是要运行这个类型的基本结构,也就是类Person的基本结构。
由于类是我们自己构造的,那么基本结构也是我们自己手动构造的。在类中,基本结构是写在__init__()这个函数里面。故这个函数称为构造函数,担负着对类进行初始化的任务。
但这并不意味着一旦创建好类,就会运行构造函数,而是需要“将类实例化”,也就是说,我们必须要根据类创建一个具体的对象,才会运行__init__()函数。

fir = Person("小郭",20,"女“)

上述语句就是将Person类实例化,也就是在内存中创建了一个对象,这个对象的类型是Person类型,通过赋值语句,与变量fir建立引用关系。

如果不写__init__()会怎样?

注意,如果编写时没有为该类定义任何构造方法,那么 Python 会自动为该类创建一个只包含 self 参数的默认的构造方法。
Here Insert Picture Description
运行结果Here Insert Picture Description
可以看到instance没有本应作为长方形而有的“长”“宽”属性,它的属性表是空的。
而且instance不能直接调用类中的方法,会报错。

什么是self?

此处的self,是个对象(Object),是当前类的实例。
fir = Person("小郭“,20,“女”)就相当于Person(fir,“小郭“,20,“女”)。
在Person实例化的过程中,数据"小郭”,20,"女"通过构造函数的参数已经存入到内存中,并且这些数据以Person类型的面貌存在组成一个对象,这个对象和变量 fir 建立引用关系。这个过程也可说成这些数据附加到一个实例上。这样就能够以:object.attribute的形式,在程序中任何地方调用某个数据,例如上面的程序中以fir.name得到"小郭"这个数据。这种调用方式,在类和实例中经常使用,点号“.”后面的称之为类或者实例的属性。

这是在程序中,并且是在类的外面。如果在类的里面,想在某个地方使用传入的数据,怎么办?

在类内部,我们会写很多不同功能的函数,这些函数在类里面有另外一个名称——方法。那么,通过类的构造函数中的参数传入的这些数据也想在各个方法中被使用,就需要在类中长久保存并能随时调用这些数据。为了解决这个问题,在类中,所有传入的数据都赋给一个变量,通常这个变量的名字是self。
在构造函数中的第一个参数self,就是起到了这个作用——接收实例化过程中传入的所有数据,这些数据是通过构造函数后面的参数导入的。显然,self应该就是一个实例(准确说法是应用实例),因为它所对应的就是具体数据。

面向对象的特性

Here Insert Picture Description
属性用来描述对象的静态特征,用object.attribute 的语法来调用。
方法用来描述对象的动态特征,用object.function()的语法来调用。

比如一头家猪,它的属性(静态特征)是:粉色的,胖的,四条腿,一根尾巴等,它的方法(动态特征)是:会跑,会吃,会睡觉等。
面向对象有三大特性,分别为:封装性,继承性,多态性。下面分别对其做出简单介绍。

封装性

封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容。
所以,在使用面向对象的封装特性时,需要:
1.将内容封装到某处
2.从某处调用被封装的内容
这个”某处“既可以指在类的内部,也可以指在类的外部。
在类的外部调用被封装的内容,此时已经完成对类的实例化(也就是创建了一个对象),调用时直接object.attribute或object.function();如果是在类的内部,则利用self间接调用。

封装使数据和加工该数据的方法(函数)封装为一个整体,以实现独立性很强的模块,使得用户只能见到对象的外特性(对象能接受哪些消息,具有那些处理能力),而对象的内特性(保存内部状态的私有数据和实现加工能力的算法)对用户是隐蔽的。

继承性

继承是类与类之间的关系,是一种创建新的类的方式,新创建的叫子类,继承的叫父类(超类、基类)。子类可以使用父类的属性,方法。通过使用面向对象的继承性,可以减少代码冗余、提高重用性。
语法是:class 类名(父类)
如果定义时并未显式指定这个类的直接父类,则这个类默认继承 object 类。
关于python中的多继承,可参考
python中的多继承

多态性

Objects made according to the operation of the received message. The same message to different objects may produce entirely different course of action to accept, a phenomenon known as polymorphism. Polymorphism user can send a common message, and all of the implementation details are left to the discretion of the object receiving the message, and if so, you can call the same message in different ways. For example: Print Method Print message is called a time map or table to a printing method and transmits the same message to a text file Print invoked will be completely different . Polymorphism of inheritance is supported, the use of class inheritance hierarchy, the agreement has the general function stored in the class hierarchy as high as possible, and the different ways to achieve this functionality placed in a lower level, Thus, generated at these low-level objects can be common to different response messages.

References:
Object-oriented programming is very simple
to create an instance together with one of the old school of writing Python-
oriented and process-oriented nature of the object of difference

Problem to be solved:
the difference 1.python functional programming and process-oriented programming?
2. There is no constructor in the class, after you create an object, why object.function () not working? The class.function () but can run?

Published 29 original articles · won praise 3 · Views 961

Guess you like

Origin blog.csdn.net/weixin_44813932/article/details/103618698
Recommended