Python Operations Development (VII)----Object-oriented (top)

Source: Internet
Author: User

Content directory:

    • Object-oriented application scenarios
    • Creation of classes and objects
    • __init__ construction method in a class
    • Self understanding
    • Three main features of object-oriented: encapsulation, inheritance, polymorphism
Overview
    • 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.

Object-oriented application scenarios

When some functions have the same parameters, object-oriented methods can be used to encapsulate the value of the parameter once to the object, and then to the object to take a value

Creation of 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
    • Functions in a class The first argument must be self
    • A function defined in a class is called a "method"
# Create classes Class SQLHELPER:    def fetch (self,sql):        print (obj.hhost)        print (obj.username)        print (OBJ.PASSWD)        print (SQL)    def create (self,sql):        pass    def remove (self,sql):        pass    def modify (self,sql):        pass# creates objects based on class SQLHELPER objobj = SQLHELPER () obj.hhost = ' c1.salt.com ' #相同的参数封装在对象中obj. Username = ' root ' obj.passwd = ' 123 ' Obj.fetch (' select * from A; ') #执行fetch方法
__init__ construction method in a class

In classes with __init__ construction methods, we can usually write common parameters to the Init method

Class Foo:    def __init__ (self):        print (' Automatically executed ')        self.hhost = ' c1.salt.com ' #将hhost变量写入到init方法中        Self.username = ' root ' #将username variable written to the Init method        self.pwd= ' 123 ' #将pwd变量写入到init方法中obj = Foo () #输出: Automatically executed

Application

Class Foo:    def __init__ (self,a1,a2,a3):  #通过传参来写init方法        print (' Automatically executed ')        self.hhost = A1        self.username = a2        self.root = A3        print (A1,A2,A3)    def fetch (self):        pass    def remove (self):        Pass    def modify (self):        pass    def create (self):        passobj1 = Foo (' c1.salt.com ', ' root1 ', ' 123 ') Obj2 = Foo (' c2.salt.com ', ' root2 ', ' 123 ')

Self's understanding  

Self is a parameter that Python automatically assigns values to

Which object executes the method, who is self

Obj1.fetch (' select * .... ') self = obj1obj2.fetch (' select * .... ') self = obj2
Three main features of object-oriented

The three major characteristics of object-oriented are: encapsulation, inheritance and polymorphism.

1. Encapsulation

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, the following actions are required:

    • Encapsulate content somewhere

    • To invoke the encapsulated content from somewhere

You can understand the encapsulation from the examples in the following illustration

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 of which encapsulates the name and age, and the previously said "content is encapsulated somewhere" which is similar to saving in content.

Practice:

1. Create three game characters, respectively:

    • Cang Jing, Female, 18, initial combat 1000

    • Tony Wood, Male, 20, initial combat 1800

    • Wave Toto, female, 19, initial combat 2500

2, the game scene, respectively:

    • Bush fights, consumes 200 combat power

    • Self-cultivation, increased 100 combat effectiveness

    • Multiplayer game, consumes 500 combat power

#-*-Coding:utf-8-*-# ##################### defines the implementation function class ##################### class Person:def __init__ (Self, NA, g En, age, fig): Self.name = na Self.gender = Gen Self.age = Age self.fight =fig def Grassla nd (self): "" "Note: Bush fights, consumes 200 combat effectiveness" "" "Self.fight = self.fight-200 def Practice (self):" "" Comment: Ego cultivation, growth                 100 Combat Effectiveness "" "Self.fight = self.fight + def incest (self):" "" Note: Multiplayer, consumes 500 combat "" " Self.fight = self.fight-500 def detail (self): "" "Comment: Details of the current object" "temp =" Name:%s; Gender:%s; Age:%s; Fighting capacity:%s "% (Self.name, Self.gender, Self.age, self.fight) Print Temp # ##################### start Game ###### ############### Cang = person (' Cang Jing ', ' female ', 18, 1000) # Create Cang jing Role dong = person (' Tony Wood ', ' man ', 20, 1800) # create Tony Wood character Bo = Perso N (' wave toto ', ' female ', 19, 2500) # Create wave toto character cang.incest () #苍井空参加一次多人游戏dong. Practice () #东尼木木自我修炼了一次bo. Grassland () #波多多参加一次草丛战斗 # Output details of the current owner Cang.detail () dOng.detail () Bo.detail () cang.incest () #苍井空又参加一次多人游戏dong. Incest () #东尼木木也参加了一个多人游戏bo. Practice () #波多多自我修炼了一次 # Output details of the current owner Cang.detail () Dong.detail () Bo.detail ()

2. Inheritance

Inheritance, the inheritance in object-oriented is the same as the inheritance in real life, that is, the child can inherit the parent's content.

If we were to create a class for both cats and dogs, we would need to implement all of their functions for cats and dogs, as shown here:

Common inheritance

Multiple inheritance

Reference url:http://www.cnblogs.com/wupeiqi/p/4493506.html

Python Operations Development (VII)----Object-oriented (top)

Related Article

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.