Python Learning Journey-Basic (eight) object oriented

Source: Internet
Author: User

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 ..."

Comparison example: Functional and object-oriented programming

Connection database additions and deletions

#function-type programmingdefSlect (host, username, password, sql):PassdefCreate (host, username, password, database):PassdefRemove (host, username, password, table):PassdefModify (host, username, password, nid):Pass#calling Functionshostname ="db01.renrenche.com"User="Admin"pwd="123"Slect (hostname, user, PWD,"SELECT * from DB1") Create (hostname, user, PWD,"DB1") Remove (hostname, user, PWD,"table1") Modify (hostname, user, PWD,"id01")
#Object-Oriented programming#01 Creating a classclassSqlheper:defslect (Self, SQL):Print(Self.hostname)Print(SQL)defCreate (self, database):Pass    defRemove (self, table):Pass    defModify (Self, nid):Pass#02 Creating Objectsobj =Sqlheper ()#03 Defining Public parametersObj.hostname ="db01.renrenche.com"Obj.user="Admin"obj.pwd="123"#04 Methods for calling ClassesObj.slect ("SELECT * from DB1") Obj.create ("DB1") Obj.remove ("table1") obj.modify ("ID")

I. 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

Two, object-oriented three major features:

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

1. Encapsulation

Introduce the __init__ () method:

# 01 Creating Classes Class Sqlheper:    def __init__(self, host, username, password):        self.hostname = host        Self.user = username        self.pwd = password    def slect (Self, SQL): Print(self.hostname) print (SQL) def create (Self, database): Pass def remove (self, table): Pass def modify (Self, nid): pass# 02 Gen Built Object obj1 = Sqlheper ("test1.rrc.com", "Admin01", "123",) Obj2 = Sqlheper ("test2.rrc.com", "admin02", "456",) # 03 Method Obj1.slect ("SELECT * from DB1") of the Calling classObj2.slect ("SELECT count (*) from DB2")      

Not only can you encapsulate normal parameters, you can encapsulate any parameters

#the object of the class can encapsulate any parameterclassC1:def __init__(self, Name, obj): Self.name=name Self.obj1=objclassC2:def __init__(self, job, age): Self.job=Job Self.age= AgedefShow (self):Print(self.job)classC3:def __init__(Self, AAA): Self.abc=Aaac2_obj= C2 ("IT", 18) C1_obj= C1 ("Pesen", C2_obj)Print(c1_obj.obj1.age) c3_obj=C3 (C1_obj)#Call C2 's Show method via C3C3_obj.abc.obj1.show ()
encapsulation of a class

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.

In the case of object-oriented inheritance, it is actually the method of extracting the common methods of multiple classes into the parent class, in which the subclass inherits only the parent class without having to implement each method.

classFoo1:#Foo1 is the parent class of Foo2 (base class)    defShow (self, name):Print("F1.show", name)classFoo2 (FOO1):#Foo2 is a subclass of Foo1 (derived classes)    def __init__(self, name): Self.name=namedefBar (self):Print("Bar")    defShow (self, name):Print("F2.show", name) obj= Foo2 ("AAA") Obj.bar () obj.show ("Pesen")#Prioritize the way you do it

Python has a special inheritance method that is multiple inheritance

Python Learning Journey-Basic (eight) 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.