Python:继承、单继承 和 多继承

2020-07-20  本文已影响0人  dex0423

1. 继承


>>> class Animal():                 # 创建一个父类 Animal 
...     def __init__(self):
...         print("this is an animal")
...     def swim(self):             # 创建 swim 方法
...         print("animal swim")
...
>>> class Cat(Animal):              # 创建一个子类 Cat,继承自 Animal 父类 
...     def __init__(self):
...         super().__init__()      # 使用 super().__init__() 继承 Animal 父类的所有方法,包括__init__ 方法和 swim 方法
...     def walk(self):             # 创建 walk 方法,该方法在 Cat 子类内部创建,与 Animal 父类无关 
...         print("cat walk")
...
>>> cat = Cat()                     # 实例化 Cat 子类
this is an animal                   # Cat 子类继承了 Animal 父类的所有属性和方法,所以才会打印 this is an animal
>>> cat.walk()                      # cat 实力调用 Cat 子类独有的 walk 方法
cat walk
>>> cat.swim()                      # 注意:cat 实例可以调用 Animal 父类的 swim 方法,而这个方法 Cat 子类中并未创建 
animal swim

-- 在上面的代码中,Cat 就是子类,Animal 就是父类,Cat 继承了 Animal 的 【全部】 属性和方法;
-- Cat 子类继承了 Animal 父类的 __ init __() 方法,所以在用 cat 对 Cat 类进行实例化时,才会打印 this is an animal;
-- Cat 子类继承了 Animal 父类的 swim() 方法,所以尽管 Cat 子类中没有 swim() 方法,但是 Cat 的实例 cat 仍然可以调用 swim() 方法;

2. 单继承


>>> class Animal():                        # 创建父类 Animal
...     def __init__(self):                # 初始化,打印 this is an animal
...         print("this is an animal")
...     def swim(self):
...         print("animal swim")
...     def walk(self):
...         print("animal walk")
...     def fly(self):
...         print("animal fly")
...
>>>
>>> class Cat(Animal):
...     def walk(self):                     # 重写 walk 方法
...         print("cat walk")
...
>>>
>>> class Fish(Animal):
...     def swim(self):                    # 重写 swim 方法
...         print("fish swim")
...
>>>
>>> class Bird(Animal):
...     def swim(self):                    # 重写 swim 方法
...         print("bird swim")
...     def walk(self):                    # 重写 walk 方法
...         print("bird walk")
...     def fly(self):                     # 重写 fly方法
...         print("bird fly")
...
>>>
>>> white_cat = Cat()
this is an animal                          # 实例化 Cat 类时,打印 this is an animal,这是继承自 Animal 父类的初始化动作
>>> white_cat.walk()
cat walk
>>>
>>>
>>> golden_fish = Fish()
this is an animal                          # 实例化 Fish 类时,打印 this is an animal,这是继承自 Animal 父类的初始化动作
>>> golden_fish.swim()
fish swim
>>>
>>>
>>> blue_bird = Bird()
this is an animal                          # 实例化 Bird 类时,打印 this is an animal,这是继承自 Animal 父类的初始化动作
>>> blue_bird.walk()
bird walk
>>> blue_bird.swim()
bird swim
>>> blue_bird.fly()
bird fly

3. 多继承


>>> class Animal():
...     def __init__(self):
...         print("this is an animal")
...
>>> class SwimAnimal(Animal):
...     def __init__(self):
...         super().__init__()
...         print("this is swim animal")
...     def swim(self):
...         print("animal swim")
...
>>>
>>> class WalkAnimal(Animal):
...     def __init__(self):
...         super().__init__()
...         print("this is walk animal")
...     def walk(self):
...         print("animal walk")
...
>>>
>>> class FlyAnimal(Animal):
...     def __init__(self):
...         super().__init__()
...         print("this is fly animal")
...     def fly(self):
...         print("animal fly")
...
>>>
>>> class Cat(WalkAnimal):
...     def __init__(self):
...         super().__init__()
...
>>>
>>> class Fish(SwimAnimal):
...     def __init__(self):
...         super().__init__()
...
>>>
>>> class Bird(SwimAnimal, WalkAnimal, FlyAnimal):
...     def __init__(self):
...         super().__init__()
...
>>>
>>> white_cat = Cat()
this is an animal
this is walk animal
>>> white_cat.walk()
animal walk
>>>
>>>
>>> golden_fish = Fish()
this is an animal
this is swim animal
>>> golden_fish.swim()
animal swim
>>>
>>>
>>> blue_bird = Bird()
this is an animal
this is fly animal
this is walk animal
this is swim animal
>>> blue_bird.walk()
animal walk
>>> blue_bird.swim()
animal swim
>>> blue_bird.fly()
animal fly

-- 上面的代码中,SwimAnimal、WalkAnimal、FlyAnimal 三个类继承自 Animal 父类,Cat 类继承自 WalkAnimal,Fish 类继承自 SwimAnimal,Bird 类继承自 SwimAnimal, WalkAnimal, FlyAnimal 三个类;
-- 图示如下:

image.png

-- 其中,Bird 类继承自 SwimAnimal, WalkAnimal, FlyAnimal 三个类,而这三个类又共同继承自 Animal 父类,所以实例化 Bird 类时,打印了 this is an animal、this is fly animal、this is walk animal、this is swim animal 四句话;

上一篇 下一篇

猜你喜欢

热点阅读