Python Object-oriented

Source: Internet
Author: User

First, the package


#面向对象: Encapsulation
Class Person:
def __init__ (self,name,age):
"""
constructor, which is executed automatically when the class is called. Can be used to encapsulate a variety of common parameters and so on.
Self: The object that invokes the caller of the method is who is who
"""
Self.name = Name
Self.age = Age
Self.gender = "Man"
Def show (self):
Print ("He is%s, age:%s"% (Self.name,self.age))

P1=person ("Xiaofan", 20)
P1.show ()

Ii. inheritance

#面向对象: Inheritance
Class F: #父类, base class
def F1 (self):
Print (' F.f1. Self:%s '%self) # Call the parent to send him self, either automatically or manually
def f2 (self):
Print (' F.f2 ')

Class S (f): # Subclass, derived class, inherited from F. Look in your own class first, no more in the parent class.
def s1 (self):
Print (' s.s1 ')
def f2 (self): #重写
Pass
def f3 (self):
Super (S,self). F2 ()
#方法1: Executes the F2 method in the parent class. Super (subclass name, caller self)
F.f2 (self)
# Method 2: Execute the F2 method in the parent class. Execute object self automatically create, perform classification methods you have to give him self.
Print (' s.f3 ')
A = S ()
A.S1 ()
A.f1 () # Execute Parent class method
A.F2 ()
A.F3 ()

# Python Multiple inheritance:
# when the same method is in the parent class, the inheritance order takes precedence from left to right and is stopped when found
# Inheritance: C , B, a--D
# Inheritance Order: From left (all left)--right
# inheritance: Z , C, B, a-D
# Inheritance Order: From left (top Z, not)--right----and finally find Z


#多继承:
Class Basereuqest:
def __init__ (self):
Print (' Basereuqest.init ')
Class RequestHandler (Basereuqest):
def __init__ (self):
Print (' Requesthandler.init ')
Basereuqest.__init__ (self)
def serve_forever (self):
Print (' Requesthandler.serve_forever ')
Self.process_request ()
#此处会调用哪个process_request ()?
# Self, is the obj, obj== caller. Then it must be the execution of Process_request () in Minx.
# Pay attention to what class of self is the object, then go back there and look for
def process_request (self):
Print (' Requesthandler.process_request ')
Class Minx:
def process_request (self):
Print (' Minx.process_request ')
Class Son (Minx, RequestHandler):
Pass
Obj=son ()
# Instantiate son's object, son () inherit Minx, and RequestHandler.
# initialization method for executing RequestHandler
Obj.serve_forever ()
# Initialize method to execute RequestHandler parent class Basereuqest
# Execute RequestHandler's Server_forever ()
# Process_request () method for executing Minx

Three, polymorphic

#多态: Parameters passed in different types of methods are executed differently
#在python中不需要考虑多态, Python native is polymorphic.

Python Object-oriented

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.