Python Object-oriented

Source: Internet
Author: User

Python Polygon Object Concepts
    • 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.

1 2 3 4 5 6 7 8 9 Ten One A - - the - - - while True    if cpu利用率 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接     if 硬盘使用空间 > 90%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接     if 内存占用 > 80%:        #发送邮件提醒        连接邮箱服务器        发送邮件        关闭连接

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

1 2 3 4 5 6 7 8 9 Ten One A - - the - def 发送邮件(内容)    #发送邮件提醒    连接邮箱服务器    发送邮件    关闭连接 while True     if cpu利用率 > 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
A class is a combination of a feature (the definition of a variable) that is common to a series of objects and a skill (the definition of a function).
Class Foo:    def Bar (self):        print (123)    def Hello (self,name):        print (name) F = Foo () F.hello (' 123 ')
Object-oriented three major features

The three major characteristics of object-oriented 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

 

#!/usr/bin/python#-*-coding:utf-8-*-class Foo:    def __init__ (self,name,age): #Foo类的构造方法        Self.name=name        self.name=name    def cc (self):        print (self.name)        return Selfobj1=foo (' Han ', +) #创建Foo类对象

  

Exercise one: At the terminal output the following information Xiao Ming, 10-year-old, male, go up the hill to firewood Xiao Ming, 10-year-old, male, driving to the northeast Xiao Ming, 10-year-old, male, love Big health old Lee, 90 years old, male, go up to the mountains to cut wood Old Lee, 90 years old,

Class Foo:

def __init__ (self, name, age, gender):
Self.name = Name
Self.age = Age
Self.gender = Gender

def Kanchai (self):
Print "%s,%s years old,%s, go up the hill to cut wood"% (Self.name, self.age, Self.gender)

def qudongbei (self):
Print "%s,%s years old,%s, drive to Tohoku" (Self.name, Self.age, Self.gender)

def Dabaojian (self):
Print "%s,%s years old,%s, favorite big health"% (Self.name, self.age, Self.gender)


Xiaoming = Foo (' xiaoming ', 10, ' male ')
Xiaoming.kanchai ()
Xiaoming.qudongbei ()
Xiaoming.dabaojian ()

Laoli = Foo (' Lao Li ', 90, ' Male ')
Laoli.kanchai ()
Laoli.qudongbei ()
Laoli.dabaojian ()

Object oriented

Object usage: Only one, is the attribute reference # instantiation class Chinese:    country = ' China '    # chinese.__init__ (P1, ' Egon ', ' + ', ' Male ')    def __ Init__ (self, name, age, Sex):        # P1. Name=name;p1. Age=age,p1. Sex=sex self        . Name = name Self        . Age = Age Self        . sex = Sex    def talk (self):        print ('%s is talking '%self. Name) # P1=chinese (' Egon ', ' Male ') #Chinese. __init__ (P1, ' Egon ', ' + ', ' Male ') # P2=chinese (' Alex ', ' 9000 ', ' female ') ) #Chinese. __init__ (P1, ' Egon ', ' + ', ' Male ') #对象的使用: Only one is the attribute reference # print (P1. Name) # print (P1. Age) # Print (P1. SEX) # Print (P2. Name) # Print (p1.country) # print (p2.country)

  

  

  

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.