Learn Python in one minute | Object-oriented (Chinese)

[ 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

Class method

Inside the class, use the def keyword to define a method, which is different from the general function definition

  1. The class method must contain the parameter self, which is the first parameter. Self represents an instance of the class.
  2. The method call needs to instantiate the class and call it in the form of "instance name. method name (parameter list)"
  3. Must be indented by one unit as a whole
    The following is a code example
# 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.

Inheritance

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

Multiple inheritance

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 &quot;pdf&quot;
No routine to receive a full set of original Django and flask tutorials ⬇️

Recommended Posts

Learn Python in one minute | Object-oriented (Chinese)
Learn Python in one minute | Python functions (on)
Learn Python in One Minute | Object Oriented (Part 1)
Learn the hard core operation of Python in one minute
What is object-oriented in python
Learn about garbage collection in Python
Python review one
Functions in python
Python object-oriented example
One picture flow: all built-in exceptions in Python
One article to get regular expressions in Python
Python object-oriented basics
Python object-oriented magic method
Join function in Python
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Install python in Ubuntu
Learn about Python3 coroutine
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python