Three Object Oriented Features
- Encapsulation encapsulates attributes and methods into an abstract class according to duties
- Inheritance enables code reuse without requiring duplicate writing of the same code
- Polymorphic. Different objects call the same methods, produce different execution results, and increase code flexibility
01. Single Inheritance
1.1 Concepts, grammar and features of inheritance
The concept of inheritance: subclasses have all methods and properties of the parent class
Develop animals and dogs without inheritanceclass 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("Wow") # Create an object - Dog Object wangcai = Dog() wangcai.eat() wangcai.drink() wangcai.run() wangcai.sleep() wangcai.bark()