Python object-oriented inheritance cases

Three characteristics of object-oriented

  1. Package  according to  mandate  the  attributes  and  methods  packaged  into an abstract  class  in
  2. Inheritance  to reuse implementation code , the same code need not be repeated written
  3. Polymorphic  different object calls the same method, generating different results of increasing the flexibility of the code

01. single inheritance

1.1 The concept of inheritance, syntax and features

The concept of inheritance : a subclass  has a  parent class  all the  methods  and  properties

class Animal:

    def eat(self):
        print("")

    def drink(self):
        print("")

    def run(self):
        print("")

    def sleep(self):
        print("")


class Dog:

    def eat(self):
        print("")

    def drink(self):
        print(" Drink " ) 

    DEF RUN (Self):
         Print ( " Run " ) 

    DEF SLEEP (Self):
         Print ( " sleep " ) 

    DEF Bark (Self):
         Print ( " bark " ) 

# create an object - a dog objects 
wangcai = Dog () 

wangcai.eat () 
wangcai.drink () 
wangcai.run () 
wangcai.sleep () 
wangcai.bark ()
Do not use inheritance and development of animal dogs
1) inheritance syntax
class class name (parent 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): 

    # 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 ( " bark " ) 

# create an object - the object dog 
wangcai = dog () 

wangcai.eat () 
wangcai.drink () 
wangcai.run () 
wangcai.sleep () 
wangcai.bark ()
Using inheritance and development of animal dogs
  • Subclass  inherits from  a parent class , you can directly  enjoy  the parent class method has been packaged, the need to develop again
  • Subclasses  should According  functions , package  subclass-specific properties and methods
2) Terminology
  • Dog Class is a  Animal class subclass , Animal class is the  Dog class of the parent class , Dog class from  Animal class inherits
  • Dog Class is the  Animal class of the derived class , Animal the class is a  Dog class of the base class , Dog class from  Animal class derived
Transitive 3) inheritance
  • C Class from  B a class with B class and from  A class inheritance
  • Then the  C class will have a  B class and  A all the properties and methods of the class

Subclass  has the  parent  and  the parent's parent class  of all the packages in  the properties  and  methods

class Animal:

    def eat(self):
        print("吃---")

    def drink(self):
        print("喝---")

    def run(self):
        print("跑---")

    def sleep(self):
        print("睡---")


class Dog(Animal):

    def bark(self):
        print("汪汪叫")


class XiaoTianQuan(Dog):

    defFly (Self):
         Print ( " I can fly " ) 


class Cat (Animal): 

    DEF the catch (Self):
         Print ( " catch mice " ) 

# create a deified dog object 
XTQ = XiaoTianQuan () 

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

xtq.catch ()
Transitive inheritance

 

Question: deified dog  can call the  Cat class defined in the  catch way?

Answer: can not , because the  deified dog  and  Cat not between the  inheritance  relationship.

 

1.2 Method of rewriting

  • Subclass  has a  parent class  all the  methods  and  properties
  • Subclass  inherits from  a parent class , you can directly  enjoy  the parent class method has been packaged, the need to develop again

Scenarios

  • When  the parent class  when the implementation of the method can not meet the needs of the subclass, a method may be  rewritten (the override)

    

Override the  parent class method there are two cases:

  1. Covering the  parent class method
  2. Methods of the parent class  extend
1) covering the parent class method
  • If in development, the parent class implementation  and  subclass implementation , completely different
  • It can be used  to cover  a manner, in the subclass  to rewrite  the parent class implementation

  Specific implementation, the equivalent  subclasses  defines a  and the same name as the parent class and implements

 After rewriting, at run time, just call the  method in the subclass rewritten, and will no longer call the  parent class package

class Animal:

    def eat(self):
        print("吃---")

    def drink(self):
        print("喝---")

    def run(self):
        print("跑---")

    def sleep(self):
        print("睡---")


class Dog(Animal):

    def bark(self):
        print("汪汪叫")


class XiaoTianQuan(Dog):

    def fly(self):
        print("我会飞")

    def bark(self):
        print("叫得跟神一样...")


xtq = XiaoTianQuan()

# 如果子类中,重写了父类的方法
# 在使用子类对象调用方法时,会调用子类中重写的方法
xtq.bark()
覆盖父类方法

 

Guess you like

Origin www.cnblogs.com/huiyichanmian/p/11286342.html