Class properties
Instance attributes
class Car:
number=4 # The number of passengers
def __init__(self,name):
self.name=name
car1=Car('audi rs4')
print(car1.name)
print(car1.number)
Car.number=7 # Modify class properties
print(car1.number)
>>>audi rs4
>>>4
>>>7
def eat(self):
print(' The students are eating ...')
@staticmethod
def sm(): # No, self
print(' Static methods ')
# call
Class name .method()
@classmethod
def cm(cls): # Pass in cls
print(' Class method ')
# call
Class name .method()
def __init___(self,name):
self.name=name #self.name Called instance properties
# Object name . Method name ()
# Class name . Method name ( Object name )
class Car:
number=4 # The number of passengers
def __init__(self,name):
self.name=name
def show(self): # To call instance properties , Remember to introduce self
print(' The car model is '+self.name)
car1=Car('audi rs4')
car1.show()
Car.show(car1)
>>> The car model is audi rs4
>>> The car model is audi rs4
# Dynamic binding method
class Car:
number=4 # The number of passengers
def __init__(self,name):
self.name=name
def show(self):
print(' The car model is '+self.name)
def wash():
print(' Cleaning completed ')
car1=Car('audi rs4')
car1.wash=wash
car1.wash()
>>> Cleaning completed
What are the functions of the three features
effect : Improve the security of the program
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(' full name :{0}, Age :{1}'.format(self.name,self.age))
class Student(Person):
def __init__(self,name,age,sex):
super().__init__(name,age)
self.sex=sex
stu=Student('wang',12,'male')
stu.info()
>>> full name :wang, Age :12
super After a ()
Multiple inheritance demo
Method rewriting
Subclasses can rewrite a property or method inherited from the parent class , When writing, you can also use super() call .
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(' full name :{0}, Age :{1}'.format(self.name,self.age))
class Student(Person):
def __init__(self,name,age,sex):
super().__init__(name,age)
self.sex=sex
def info(self):
super().info()
print(' Gender :{0}'.format(self.sex))
stu=Student('wang',12,'male')
stu.info()
polymorphic 、 polymorphism 、 The duck type
python The understanding of polymorphism in - Yu Wenzhe's article - You know https://zhuanlan.zhihu.com/p/88402677
The name of this concept comes from James Whitcomb Riley Proposed duck test ,“ Duck test ” It can be expressed in this way :“ When you see a bird walk like a duck 、 Swimming like a duck 、 It also sounds like a duck , Then this bird can be called a duck .”
class Duck():
def walk(self):
print('I walk like a duck')
def swim(self):
print('i swim like a duck')
class Person():
def walk(self):
print('this one walk like a duck')
def swim(self):
print('this man swim like a duck')
It's obvious ,Person Class has and Duck Class , When there is a function call Duck class , And used two methods walk() and swim(). We passed Person Classes can also run , The function does not check if the object's type is Duck, As long as he has walk() and swim() Method , Can be called correctly .
To be continued !