Article directory
- 1. Do not use inheritance to develop animals and dogs
- 2. Use inheritance to develop animals and dogs
- 3. Transitivity of inheritance
- 4. Precautions for inheritance and transmission
- 5. Method of overriding parent class
- 6. Methods to expand the parent class
- 7. Private properties and private methods of the parent class
- 8. Common method of parent class
- 9. more inheritance
- 10. Precautions for multi inheritance use
- 11. Polymorphic cases
1. Do not use inheritance to develop animals and dogs
class Animal: def eat(self): print("eat") def drink(self): print("drink") def run(self): print("run") def sleep(self): print("sleep") class Dog: def eat(self): print("eat") def drink(self): print("drink") def run(self): print("run") def sleep(self): print("sleep") def bark(self): print("Bark") # Create an object - Dog object wangcai = Dog() wangcai.eat() wangcai.drink() wangcai.run() wangcai.sleep() wangcai.bark()
2. Use inheritance to develop animals and dogs
class Animal: def eat(self): print("eat---") def drink(self): print("drink---") def run(self): print("run---") def sleep(self): print("sleep---") class Dog(Animal): # A subclass has all the properties and methods of the parent # def eat(self): # print("eat") # # def drink(self): # print("drink") # # def run(self): # print("run") # # def sleep(self): # print("sleep") def bark(self): print("Bark") # Create an object - Dog object wangcai = Dog() wangcai.eat() wangcai.drink() wangcai.run() wangcai.sleep() wangcai.bark()
3. Transitivity of inheritance
class Animal: def eat(self): print("eat---") def drink(self): print("drink---") def run(self): print("run---") def sleep(self): print("sleep---") class Dog(Animal): def bark(self): print("Bark") class XiaoTianQuan(Dog): def fly(self): print("I can fly.") # Create an object for a dog xtq = XiaoTianQuan() xtq.fly() xtq.bark() xtq.eat()
4. Precautions for inheritance and transmission
class Animal: def eat(self): print("eat---") def drink(self): print("drink---") def run(self): print("run---") def sleep(self): print("sleep---") class Dog(Animal): def bark(self): print("Bark") class XiaoTianQuan(Dog): def fly(self): print("I can fly.") class Cat(Animal): def catch(self): print("Catch mice") # Create an object for a dog xtq = XiaoTianQuan() xtq.fly() xtq.bark() xtq.eat() xtq.catch()
5. Method of overriding parent class
class Animal: def eat(self): print("eat---") def drink(self): print("drink---") def run(self): print("run---") def sleep(self): print("sleep---") class Dog(Animal): def bark(self): print("Bark") class XiaoTianQuan(Dog): def fly(self): print("I can fly.") def bark(self): print("Call like a God...") xtq = XiaoTianQuan() # If the method of the parent class is overridden in the subclass # When a method is called with a subclass object, the overridden method in the subclass is called xtq.bark()
6. Methods to expand the parent class
class Animal: def eat(self): print("eat---") def drink(self): print("drink---") def run(self): print("run---") def sleep(self): print("sleep---") class Dog(Animal): def bark(self): print("Bark") class XiaoTianQuan(Dog): def fly(self): print("I can fly.") def bark(self): # 1. Write code for the specific requirements of subclass print("God like call...") # 2. Use super(). To call the method originally encapsulated in the parent class # super().bark() # Parent class name. Method (self) Dog.bark(self) # Note: if a subclass is used to call a method, a recursive call dead loop will occur! # XiaoTianQuan.bark(self) # 3. Add code of other subclasses print("$%^*%^$%^#%$%") xtq = XiaoTianQuan() # If the method of the parent class is overridden in the subclass # When a method is called with a subclass object, the overridden method in the subclass is called xtq.bark()
7. Private properties and private methods of the parent class
class A: def __init__(self): self.num1 = 100 self.__num2 = 200 def __test(self): print("Private method %d %d" % (self.num1, self.__num2)) class B(A): def demo(self): # 1. The private property of the parent class cannot be accessed in the object method of the child class # print("access the private property% d" of the parent class% self. \ # 2. In the object method of the subclass, the private method of the parent class cannot be called # self.__test() pass # Create a subclass object b = B() print(b) b.demo() # Can't directly access the private properties of the object / call private methods outside # print(b.__num2) # b.__test()
8. Common method of parent class
class A: def __init__(self): self.num1 = 100 self.__num2 = 200 def __test(self): print("Private method %d %d" % (self.num1, self.__num2)) def test(self): print("Public method of parent class %d" % self.__num2) self.__test() class B(A): def demo(self): # 1. The private property of the parent class cannot be accessed in the object method of the child class # print("access the private property% d" of the parent class% self. \ # 2. In the object method of the subclass, the private method of the parent class cannot be called # self.__test() # 3. Access the public properties of the parent class print("Subclass method %d" % self.num1) # 4. Call the public method of the parent class self.test() pass # Create a subclass object b = B() print(b) b.demo() # Access public properties of parent class / call public methods outside # print(b.num1) # b.test() # Can't directly access the private properties of the object / call private methods outside # print(b.__num2) # b.__test()
9. more inheritance
class A: def test(self): print("test Method") class B: def demo(self): print("demo Method") class C(A, B): """Multiple inheritance can make subclass objects have properties and methods of multiple parents at the same time""" pass # Create a subclass object c = C() c.test() c.demo()
10. Precautions for multi inheritance use
class A: def test(self): print("A --- test Method") def demo(self): print("A --- demo Method") class B: def test(self): print("B --- test Method") def demo(self): print("B --- demo Method") class C(B, A): """Multiple inheritance can make subclass objects have properties and methods of multiple parents at the same time""" pass # Create a subclass object c = C() c.test() c.demo() # Determine the order in which class C objects call methods print(C.__mro__)
11. Polymorphic cases
class Dog(object): def __init__(self, name): self.name = name def game(self): print("%s Bouncing play..." % self.name) class XiaoTianDog(Dog): def game(self): print("%s Fly to the sky to play..." % self.name) class Person(object): def __init__(self, name): self.name = name def game_with_dog(self, dog): print("%s and %s Happy play..." % (self.name, dog.name)) # Let the dog play dog.game() # 1. Create a dog object # wangcai = Dog("Wangcai") wangcai = XiaoTianDog("Flying wealth") # 2. Create a Xiaoming object xiaoming = Person("Xiao Ming") # 3. Let Xiaoming play with the dog xiaoming.game_with_dog(wangcai)