Python Object-Oriented Inheritance Case

Keywords: PHP

Three Object Oriented Features

  1. Encapsulation encapsulates attributes and methods into an abstract class according to duties
  2. Inheritance enables code reuse without requiring duplicate writing of the same code
  3. 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

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("Wow")

# Create an object - Dog Object
wangcai = Dog()

wangcai.eat()
wangcai.drink()
wangcai.run()
wangcai.sleep()
wangcai.bark()
Develop animals and dogs without inheritance
1) Inherited grammar
class Class name(Parent Class Name):

    pass
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 child class has all the properties and methods of the parent class
    # 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()
Develop animals and dogs using inheritance
2) Terminology
3) Transitivity of Inheritance

Subclass owns the parent class and all the properties and methods encapsulated in the parent class of 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("Wow")


class XiaoTianQuan(Dog):

    def fly(self):
        print("I can fly")


class Cat(Animal):

    def catch(self):
        print("Catching mice")

# Create an object for a wheezing dog
xtq = XiaoTianQuan()

xtq.fly()
xtq.bark()
xtq.eat()

xtq.catch()
Transitivity of Inheritance

 

Question: Can the wheezing dog call the catch method defined in the Cat class?

Answer: No, because there is no inheritance relationship between the wheezing dog and the Cat.

 

Rewrite of 1.2 Method

Application Scenarios

    

There are two cases of overriding a parent method:

  1. Override method of parent class
  2. Extend parent method
1) Override method of parent class

The implementation is equivalent to defining and implementing a method with the same name as the parent in the subclass

After overriding, at run time, only methods overridden in subclasses will be called, not methods encapsulated in parent classes

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("Wow")


class XiaoTianQuan(Dog):

    def fly(self):
        print("I can fly")

    def bark(self):
        print("Call like God...")


xtq = XiaoTianQuan()

# If the method of the parent class is overridden in a subclass
# When a method is invoked using a subclass object, the method overridden in the subclass is invoked
xtq.bark()
Override parent method

Posted by haixiao on Thu, 01 Aug 2019 13:43:55 -0700