Python Object-oriented

Source: Internet
Author: User

Concept:

    • Process oriented: Write base code from top to bottom according to business logic
    • Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
    • Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."

Process-oriented programming is most easily accepted by beginners, it often uses a long code to achieve the specified function, the most common operation in the development process is to paste the copy, that is: Copy the previously implemented code block to the current function.

123456789101112131415161718 whileTrue:    ifcpu利用率 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接    if硬盘使用空间 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接    if内存占用 > 80%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接

Over time, functional programming has been used to enhance the reusability and readability of code, and it has become:

12345678910111213141516 def发送邮件(内容)    #发送邮件提醒    连接邮箱服务器    发送邮件    关闭连接whileTrue:    ifcpu利用率 > 90%:        发送邮件(‘CPU报警‘)    if硬盘使用空间 > 90%:        发送邮件(‘硬盘报警‘)    if内存占用 > 80%:        发送邮件(‘内存报警‘) 

Today we are going to learn a new way of programming: Object-oriented Programming (object Oriented Programming,oop, OO programming)
Note: Java and C # only support object-oriented programming, while Python is more flexible in that it supports object-oriented programming and functional programming

Creating classes and Objects                                                                                                                                                                                                  

Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".

A class is a template that can contain multiple functions and functions in a template.

Objects are instances created from templates that can execute functions in a class through an instance object

    • Class is a keyword that indicates that classes
    • Create the object, and then add parentheses to the class name

PS: A function in a class The first argument must be self(see in detail: Encapsulation of the three main characteristics of a class)
A function defined in a class is called a "method"

# 创建类classFoo:        defBar(self):        print‘Bar‘    defHello(self, name):        print‘i am %s‘%name# 根据类Foo创建对象objobj = Foo()obj.Bar()            #执行Bar方法obj.Hello(‘wupeiqi‘) #执行Hello方法 

Do you have a question in here? Using functional programming and object-oriented programming to execute a "method" is easier than an object-oriented approach

    • Object-oriented: "Create Object" "Execute method by Object"
    • Function Programming: "Execute function"

Observing the above-mentioned answers is affirmative, then not absolute, and the different scenarios are different for their programming styles.

Summary: Functional application Scenarios--independent and no shared data between functions

Object-oriented three major features

The three main object-oriented features are: encapsulation, inheritance, and polymorphism

First, the package

Encapsulation, as the name implies, encapsulates the content somewhere and then calls the content that is encapsulated somewhere.

Therefore, when using object-oriented encapsulation features, you need:

    • Encapsulate content somewhere
    • To invoke the encapsulated content from somewhere

First step: encapsulate the content somewhere

Self is a formal parameter, when executing obj1 = Foo (' Wupeiqi ', 18), self equals obj1

When executing obj2 = Foo (' Alex ', 78), self equals obj2

So, the content is actually encapsulated in objects Obj1 and OBJ2, each object has a name and an age attribute, which is similar to saving in memory.

Step two: Call the encapsulated content from somewhere

When the encapsulated content is called, there are two scenarios:

    • Called directly through an object
    • Indirectly called through self

1. Direct invocation of encapsulated content by object

Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name

1 classFoo:2  3     def __init__(self, Name, age):4Self.name =name5Self.age = Age6  7Obj1 = Foo ('Wupeiqi', 18)8 PrintObj1.name#call the Name property of the Obj1 object directly9 PrintObj1.age#call the Age property of the Obj1 object directlyTen   OneObj2 = Foo ('Alex', 73) A PrintObj2.name#call the Name property of the Obj2 object directly - PrintObj2.age#call the Age property of the Obj2 object directly
object. Property name Call

2. Indirectly invoking the encapsulated content via self

When executing a method in a class, the encapsulated content needs to be indirectly called through self

1 classFoo:2 3     def __init__(self, Name, age):4Self.name =name5Self.age = Age6 7     defdetail (self):8         Print(Self.name)9         Print(self.age)Ten  OneObj1 = Foo ('Wupeiqi', 18) AObj1.detail ()#Python will pass obj1 to the self parameter by default, which is: Obj1.detail (obj1), so the self = Obj1 inside the method, that is, Self.name is Wupeiqi; Self.age is -  -Obj2 = Foo ('Alex', 73) theObj2.detail ()#Python will pass obj2 to the self parameter by default, namely: Obj1.detail (OBJ2), so the self = Obj2 inside the method, that is: Self.name is Alex; Self.age is the
Self indirectly invokes the encapsulated content

In summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.

Python Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.