Overview
First, the Python programming method
- Process-oriented programming: Based on business logic from top to bottom lei code
- Function-oriented programming: Encapsulates a function code into a function, which can be called directly in the future without having to write again
- Object-oriented Programming: classifying and encapsulating functions
Second, process-oriented programming
while True: if CPU utilization > 90%: # Send mail reminder to Connect mailbox server send mail close connection If hard disk use space >90% :# Send mail reminder to Connect mailbox server to send mail Close Connection if memory consumption > 80%: # send mail reminder Connection Mailbox server send mail close connection
Three, function-oriented programming
defSend mail (content)#Send an email reminderConnect mailbox server send mail close connection whileTrue:ifCPU Utilization > 90%: Send mail ('CPU Alarms') ifHDD Use space > 90%: Send mail ('HDD Alarm') ifMemory Footprint > 80%: Send mail ('Memory Alarms')
Object-oriented simple application
First, object-oriented overview
Object-oriented is a way of programming, which requires "class" and "object" in the way of programming, and object-oriented is actually the operation of classes and objects.
Second, create a class
############# #创建类classFoo:defBar (self):Print('Bar') defHello (self, name):Print('I am%s'%name)############# #实例化obj =Foo () obj. Bar ()#Execute Bar MethodObj. Hello ('Wupeiqi')#Execute the Hello method
Iii. Object-oriented self-explanation
Self is a formal parameter, when executing obj1 = Foo (' Alex ', 19), Self equals obj1
When executing obj2 = Foo (' Eric ', 20), self equals obj2
Self=obj
three characteristics of object-oriented
I. Object-oriented encapsulation
Overview:
Encapsulation is the encapsulation of content to a place, and then to call the encapsulated content
A simple example
############## #代码classFoo:def __init__(Self, name, age):#called a construction method that automatically executes when an object is created according to a classSelf. Name =name self. Age= Agedefinfo (self):Print("""My name is:%s My age is:%d"""%(self.) Name, self. Age)) Ansheng= Foo ("Alex", 18)#package Alex and 18 into the name and age propertiesansheng.info () xiaoming= Foo ("Eric", 30)#encapsulates Eric and 30 into the name and age propertiesXiaoming.info ()############## #结果My Name is: Anshengmy Age is: 18My name is: Xiaomingmy Age is: 30
Second, the object-oriented inheritance
Overview:
Inheritance is the child can inherit the content of the parent, so for the inheritance, is actually the multiple classes common methods extracted into the parent class, the subclass only need to inherit the parent class and do not need one by one to implement each method
A simple example
Create a class of ' people ' information, such as people have limbs, hair, eyes, ears, so write this into a class; In creating a class of Chinese and foreigners, the Chinese language is Chinese, the skin is yellow; the language of the foreigner is English, the skin is black.
###################### #代码classpeople:def __init__(self):Print("""your general features are: limbs, hair, eyes, ears""")classChina (people):definfo (self):Print("""you are Chinese, your language is Chinese, the skin is yellow""")classUs (people):definfo (self):Print("""You are an American, your language is English, the skin is black""") C=China () C.info () m=Us () m.info ()####################### #结果your general characteristics are: limbs, hair, eyes, ears you are Chinese, your language is Chinese, the skin is yellow your general characteristics are: limbs, hair, eyes, ears you are American, your language is English, the skin is black
Parent class or subclass
base class or derived class
1. Derived classes can use all the methods in the base class
2. If the derived class and the base class exist at the same time, the derived class is preferred
Multiple-inheritance lookup order in inheritance
Top class Two classes without a parent class in case
Top class Two classes with a parent class in case
members of the class
Classification of members of a class
Second, the field
Normal field: Belongs to object, called by object
static field: Belongs to class called through class
############### #代码:classProvince:country="China" #static fields, the function is to write the same fields here, saving memory, through the class to get the properties. When the code is loaded, it has been created def __init__(self,name): Self.name= Name#normal fieldHN= Province ('Henan')#creating an object from a classPrint(Hn.name)#calling normal fieldsPrint(Province.country)#calling a static field############### #结果:Henan China
Third, the method
Common method: Called by an object, requires at least one self parameter
static method: Called by class with no default parameters
Function: A static method is required if you want to write only a normal function.
Class method: Called by the class, requires at least one CLS parameter
Role: Get the class name
############################## Code:classProvince:def __init__(self,name): Self.name=namedefShow (self):#normal method, called by the object to execute Print('Common Methods') @staticmethoddefF1 (ARG):#static methods, which are executed through a class call. The function is: if you want to write a normal function, write the static method, do not need self (this is equivalent to the normal function) Print(ARG) @classmethoddefF2 (CLS):#Class method, which executes through the class call. The function is: can get the class name Print(CLS) HN= Province ('Henan')#creating an object from a classHn.show ()#calling the normal methodPROVINCE.F1 ('Static Methods')#calling a static methodPROVINCE.F2 ()#Calling class methods########################## # #结果:Common method static method<class '__main__. Province'>
Member modifiers for class
First, overview:
There are two types of members for each class
- Public members: accessible from anywhere
- Private members: Only within the class can access
Ii. defining and invoking private static fields
######################################### #代码classFoo:__classmembers="of Private" #private static fields defMembers (self):#output by calling a private static field from a method in a class Print(Foo.)__classmembers) obj= Foo ()#Create an ObjectObj. Members ()#the members method in the execution class######################################### #结果Privately owned
Third, define and invoke private normal fields
################################## #代码classFoo:def __init__(Self, URL):#How to construct a classSelf.__blog= URL#Private Normal field Print(self.)__blog)#directly in the paparazzi method does not output the incoming URLobj= Foo ("www.baidu.com")#create an object, pass in a value################################## #结果Www.baidu.com
Object-oriented Python learning series