07_ Object-oriented

Source: Internet
Author: User

1. Initial knowledge of object-oriented

1. Python support function + object-oriented two kinds of programming

2. Functional programming, object-oriented programming implementation: E-mail function

Function Type:

def mail (email, message):    print ("Go to Hair")    return true# call function mail ("[email protected]", "good Person")

Object-oriented: Create the class first, and then create the object

# Create Class:    # Method (function in Class)    def mail (self, email, message):    print ("Go to Hair")    return true# Call # 1. Create object, class name () obj = Foo () # 2. Execute method via Object Obj.mail ("[Email protected]", "good Person")

3. Classes and objects

A) Create a class

Class Name Def method name (self, XXXX) pass

b) Create an object

Object = Class name ()

c) Through the object execution method

Object. Method Name (123)

4. Database Example

function type

def fetch (host, username, password, sql):    passdef Create (host, username, password, sql):    passdef Remove (host, Username, password, nid):    passdef Modify (host, username, password, name):    Pass

Object-oriented 1

Class SQLHelper:    def fetch (self, host, username, password, sql):        pass    def create (self, host, username, password, SQL):        pass    def remove (self, host, username, password, nid):        pass    def modify (self, host, Username, password, name):        passobj = SQLHelper ()

Object-Oriented 2

Class SQLHelper:    def fetch (self, SQL):        # Connect database        print (self.hhost)        print (self.uusername)        print ( SELF.PWD)        print (SQL)    def create (self, SQL):        pass    def remove (self, nid):        pass    def modify ( Self, name):        passobj = SQLHelper () obj.hhost = "c1.salt.com" obj.uusername = "Alex" obj.pwd = "123" Obj.fetch ("SELECT * From A ")
Summary

1. Creation and execution of classes and objects

# Create Class class Name: Def method name (self, xxxx):p ass# Create Object object = Class name () # Executes a method object from an object. Method Name (123)

2. When to use object-oriented

When some functions have parameters, object-oriented methods can be used to encapsulate the parameter values one at a time to the object, then to the object to take a value.

2. What the hell is self?

Self is a parameter that Python automatically assigns values to

Which object executes the method, and who is self.

Obj1.fetch ("select * from A") self = = Obj1

Obj2.fetch ("select * from A") Self ==obj2

3. Construction Method __init__

Class has a special method __init__, the class () is automatically executed

Class SQLHelper:    # constructor method __init__,a1,a2,a3 for parameters, receive object Parameters    def __init__ (self, a1, A2, A3):        Print ("Auto-execute init")        self.hhost = A1        self.uusername = A2        self.pwd =a3    def fetch (self, SQL):        pass    def create (self, SQL):        pass    def remove (self, nid):        pass    def modify (self, name):        pass# object, object parameter obj1 = SQLHelper ("c1.salt.com", ' Alex ', ' 123 ') # Execute Object obj1.fetch ("select * from A")
4. Encapsulation of the three main features of object-oriented

Object-oriented three major features: encapsulation, inheritance, polymorphism

Package: Primary

Class Person:    def __init__ (self, name, age, Money):        self.name = name        Self.age = age        Self.money = Money
   def Shopping (self):        Self.money = Self.money-2long = person (' dragon ', ' 1 ') Hu = person (' Tiger ', ' 2 ') Bao = person (' Leopard ', 18 , ten) bao.shopping () print (Bao.money)

Encapsulating objects

Class C1:    def __init__ (self, Name, obj):        self.name = name        self.obj = objclass C2:    def __init__ (self, name , age):        self.name = name        Self.age = Age    def show (self):        print (self.name) # Create object c2_obj, Parameter c2_obj = C2 ( ' AA ', 11) # Create object C1, Parameter 2 is Object c2_obj,c2_obj of name,agec1_obj = C1 ("Alex", C2_obj) # C1_obj under obj ageprint (c1_obj.obj.age, c1_ Obj.obj.name, C1_obj.name)

Package: Final Level

Class C1:    def __init__ (self, Name, obj):        self.name = name        self.obj = objclass C2:    def __init__ (self, name , age):        self.name = name        Self.age = Age    def show (self):        print (Self.name) class C3:    def __init__ ( Self, A1):        Self.money = 123        self.aaa = a1c2_obj = C2 (' C2 ', one) # C2_obj is the C2 type #-name = "AA" #-age = 11c1_obj = C1 ("C1", C2_obj) # c1_obj is the C1 type #-name = "C1" #-obj = C2_objc3_obj = C3 (C1_obj) print (c3_obj.aaa.obj.name) # Use C2_obj to execute Show method C 3_obj.aaa.obj.show () # Print (C3.AAA)
5. Inheritance of three major characteristics of area objects

Python can inherit multiple

5.1 Single Inheritance

Single Inheritance Exercise 1:

Class F1: # parent class, base class    def show (self):        print ("show")    def foo (self):        print (Self.name) class F2 (F1):  # Subclass , derived class    def __init__ (self, name):        self.name = name    def bar (self):        print (' Bar ')    def Show (self): # The parent class and subclass have functions that take precedence to subclass local        print (' f2.show ') obj = F2 (' Alex ') Obj.show () Obj.foo ()

  

Single Inheritance Exercise 2:

Class S1:    def F1 (self): self        . F2 ()    def F2 (self):        passclass S2 (S1):    def-F3 (self): self        . F1 ()    def F2 (self):        passobj = S2 () obj. F3 () # Execution Result: S2 's f2obj = S1 () obj. F1 () # Execution Result: S1 's F2

Summary: As soon as self is present, go back to the origin and look up from the bottom.

1.5.2 Multiple Inheritance

Multiple inheritance Exercise 1: Simple formatting

Class C1:    def F1 (self):        print ("C1 F1")        Passclass C2:    def f2 (self):        print ("C2 F2")        Passclass C3 (C2,C1):    def f3 (self):        print ("C3 F3")        passobj = C3 () obj.f3 () 1obj.f2 () obj.f1 () # Output results C3 f3c2 f2c1 F1

  

Multiple inheritance: Multi-layered parent class

Class C0 ():    def f2 (self):        print ("C0.f2") class C1 (C0):    def F1 (self):        print ("C1.f1")        Passclass C2:    def f2 (self):        print ("C2.f2")        passclass C3 (C1,C2):    def f3 (self):        print (" C3.f3 ")        passobj = C3 () obj.f2 () # Output result C0.f2

  

Class C0 ():    def f2 (self):        print ("C0.f2") class C1 (C0):    def F1 (self):        print ("C1.f1")        Passclass C2 :    def f2 (self):        print ("C2.f2")        passclass C3 (C2,C1):    def f3 (self):        print ("c3.f3")        passobj = C3 () obj.f2 ()

Summary:

Multiple inheritance If there are multiple parent classes, and the top-level classes are not unique, the lookup order is left-to-right, bottom-to-top.

Find order when there is a common top-level parent class

Class c_2 ():    def f2 (self):        print ("C_2.f2") class C_1 (c_2):    def F1 (self):        print ("C_1.f1") class C0 (c_2):    def F1 (self):        print ("C0.f1") class C1 (C0):    def F1 (self):        print ("C1.f1")        Passclass C2 (c_1):    def f2 (self):        print ("C2.f2")        passclass C3 (C1,C2):    def f3 (self):        print ("c3.f3")        passobj = C3 () obj.f2 () # Output result C2.f2

07_ 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.