Object-Oriented Programming(1.1)

Bob Lee
2 min readDec 14, 2020

There are three characters about OOP characters:

  1. Inheritance()
  2. polymorphism()
  3. combination()

Inheritance: a method to create new class in Python. New Class can inherit one or many parents class. Also defined as deriving new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes

Sample Code

In Animal , “__init__” function will be used for transit parameter(name). “eat”, “drink” and “function” can be seen as dynamic attributes in this class. Check the “__bases__” function of Animal, result is “(<class ‘object’>,)”, which mean that the object can be seen super class of all classes.

In Dog, How can we assign value? Because dog is belong to animal, the “instantiate” process of dog should inherit all attributes of Animal, which mean “a dog can eat, drink, and have a function”. Check the “__bases__” function of Dog, result is “(<class ‘__main__.Animal’>,)”, which mean the object of Dog is Animal.

Comparison of inherited relationship

The funny thing is that, Dog itself own an attribute called “function” as well. Once we initiate Dog, we should give Name parameter. Then “__init__” in Animal will be executed. So the line “ Execute Animal.__init__” will appear. Also, Alex can inherit “drink” attribute of Animal; Alex can perform his own “guard” attribute; However,

“Alex.function()” will not use function of Animal because Dog itself has more priority to use it own function

Result of Code

Conclusion(Chinese): 当使用类的继承,在实例化时子类必须输入和父类一样的参数,并且可以继承父类全部属性。但在调用函数时,尽管父类有相同属性,子类会优先继承自己的属性

--

--