[ One minute to learn Python | object-oriented (on)] (http://mp.weixin.qq.com/s?__biz=MzU2NTczODU3NA==&mid=2247489693&idx=2&sn=c06ae3c0aa4e8a0fb092f9e32fa1797f&chksm=fcb675bccbc1fcaa2b95a6c2da99705df2bda0aa9d0cc22e05b3370e133f68d4e2c86a5f186c&scene=21#wechat_redirect) followed by the last Continue to learn Python's object-oriented
Inside the class, use the def keyword to define a method, which is different from the general function definition
# Class definition
classpeople:
# Define basic attributes
name =''
age =0
# Define private attributes,Private properties cannot be accessed directly outside the class
__ weight =0
# Define the construction method
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):print("%s says:I%d years old."%(self.name,self.age))
# Instantiated class
p =people('ming',10,30)
p.speak()
The output of the above program is:
ming says:I am 10 years old.
Python also supports inheritance of classes. If a language does not support inheritance, classes have no meaning. The definition of the derived class is as follows:
classDerivedClassName(BaseClassName1):<statement-1>...<statement-N>
BaseClassName (the base class name in the example) must be defined in a scope with the derived class. In addition to classes, you can also use expressions. This is very useful when the base class is defined in another module:
classDerivedClassName(modname.BaseClassName):
# Class definition
classpeople:
# Define basic attributes
name =''
age =0
# Define private attributes,Private properties cannot be accessed directly outside the class
__ weight =0
# Define the construction method
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):print("%s says:I%d years old."%(self.name,self.age))
# Single inheritance example
classstudent(people):
grade =''
def __init__(self,n,a,w,g):
# Call the parent class constructor
people.__init__(self,n,a,w)
self.grade = g
# Override the method of the parent class
def speak(self):print("%s says:I%d years old, I am reading%d grade"%(self.name,self.age,self.grade))
s =student('ken',10,60,3)
s.speak()
The output of the above program is:
ken says:I am 10 years old and I am in the 3rd grade
Python also has limited support for multiple inheritance forms. The class definition of multiple inheritance is as follows:
classDerivedClassName(Base1, Base2, Base3):<statement-1>...<statement-N>
Need to pay attention to the order of the parent class in the parentheses. If the parent class has the same method name and is not specified when the subclass is used, python searches from left to right, that is, when the method is not found in the subclass, search from left to right Whether the parent class contains methods.
# Class definition
classpeople:
# Define basic attributes
name =''
age =0
# Define private attributes,Private properties cannot be accessed directly outside the class
__ weight =0
# Define the construction method
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):print("%s says:I%d years old."%(self.name,self.age))
# Single inheritance example
classstudent(people):
grade =''
def __init__(self,n,a,w,g):
# Call the parent class constructor
people.__init__(self,n,a,w)
self.grade = g
# Override the method of the parent class
def speak(self):print("%s says:I%d years old, I am reading%d grade"%(self.name,self.age,self.grade))
# Another class, preparation before multiple inheritance
classspeaker():
topic =''
name =''
def __init__(self,n,t):
self.name = n
self.topic = t
def speak(self):print("My name is%s, I am a speaker, and the topic of my speech is%s"%(self.name,self.topic))
# Multiple inheritance
classsample(speaker,student):
a =''
def __init__(self,n,a,w,g,t):
student.__init__(self,n,a,w,g)
speaker.__init__(self,n,t)
test =sample("Tim",25,80,4,"Python")
test.speak() #The method name is the same, and the method of the parent class is called first in parentheses by default
The output of the above program is:
My name is Tim, I am a speaker, and the topic of my speech is Python
I am kuls
Welcome to join me on WeChat to exchange and learn to read more exciting articles, you can follow me!
Follow the public account and reply "pdf"
No routine to receive a full set of original Django and flask tutorials ⬇️
Recommended Posts