首页 > 技术文章 > python中的继承、多继承、超继承

longshao1239 2019-12-29 15:11 原文

【继承】

#父类有的,继承后子类可以直接调用
#父类子类都有同一个重名函数,优先子类调用自己的
#父类只能调用自己的函数
 
class RobotOne:
    def __init__(self,year,name):
        self.year=year
        self.name=name
    def walking_on_ground(self):
        print(self.name+'只能在平地上行走,有障碍物会摔倒')
    def robot_info(self):
        print('{0}年产生的机器人{1}是中国研发的'.format(self.year,self.name))

class RobotTwo(RobotOne):   #继承,二代继承了一代
    def walking_on_ground(self):
        print(self.name+'可以在平地上平稳的行走')
    def walking_avoid_block(self):  #函数拓展:继承下,子类独有的
        #我想在子类函数里调用父类的一个函数,可以直接:
        #self.robot_info()
        print(self.name+'可以避开障碍物')

#继承的类是否要用到初始化函数,看是否是从父类里继承的
t2=RobotTwo(1990,'小王')    #必传参
t2.walking_avoid_block()    #重写:子类里面的函数名与父类函数名重复时叫重写
t2.robot_info()         #二代可以调用1代的方法
t1=RobotOne(1988,'小明')
t1.walking_on_ground()

【多继承】

1.有两个父类的属性和方法,如果两个父类具有同名方法时候,调用就近原则(就近类名的那个),初始化函数也包括在内

 

 
class RobotOne:
    def __init__(self,year,name,height):
        self.year=year
        self.name=name
        self.height=height
    def walking_on_ground(self):
        print(self.name+'只能在平地上行走,有障碍物会摔倒')
    def robot_info(self):
        print('{0}年产生的机器人{1},身高{2}是中国研发的'.format(self.year,self.name,self.height))

class RobotTwo:
    def __init__(self,year):
        self.year=year
    def walking_on_ground(self):
        print(self.name+'可以在平地上平稳的行走')
    def walking_avoid_block(self):  #函数拓展:继承下,子类独有的
        print(self.name+'可以避开障碍物')
class RobotThree(RobotOne,RobotTwo):    #三代同时继承一代/二代,调用就近选择
    def jump(self):
        print(self.name+'可单膝跳跃')

#继承的类是否要用到初始化函数,看是否是从父类里继承的
t3=RobotThree(1950,'大王',100)    #创建第三代实例
t3.jump()
t3.walking_on_ground() #同时继承一代和二代同名函数,会调用就近选择
 

(2)替换三代里继承的一代和二代的位置:

 
class RobotOne:
    def __init__(self,year,name,height):
        self.year=year
        self.name=name
        self.height=height
    def walking_on_ground(self):
        print(self.name+'只能在平地上行走,有障碍物会摔倒')
    def robot_info(self):
        print('{0}年产生的机器人{1},身高{2}是中国研发的'.format(self.year,self.name,self.height))

class RobotTwo:
    def __init__(self,name):
        self.name=name
    def walking_on_ground(self):
        print(self.name+'可以在平地上平稳的行走')
    def walking_avoid_block(self):  #函数拓展:继承下,子类独有的
        print(self.name+'可以避开障碍物')
class RobotThree(RobotTwo,RobotOne):    #三代同时继承二代和一代,调用就近选择
    def jump(self):
        print(self.name+'可单膝跳跃')

#继承的类是否要用到初始化函数,看是否是从父类里继承的
t3=RobotThree(1950,'大王',100)    #创建第三代实例
t3.jump()
t3.walking_on_ground()
 
#输出报错1:
Traceback (most recent call last):
  File "/Users/tuyoo/Documents/pytext/test02/other_file/a_02.py", line 55, in <module>
    t3=RobotThree(1950,'大王',100)    #创建第三代实例
TypeError: __init__() takes 2 positional arguments but 4 were given  #改为‘大王’就好了
 
#输出报错2(调用初始化函数)
/usr/local/bin/python3.7 /Users/tuyoo/Documents/pytext/test02/other_file/a_02.py
Traceback (most recent call last):
  File "/Users/tuyoo/Documents/pytext/test02/other_file/a_02.py", line 56, in <module>
    t3.robot_info()
  File "/Users/tuyoo/Documents/pytext/test02/other_file/a_02.py", line 41, in robot_info
    print('{0}年产生的机器人{1},身高{2}是中国研发的'.format(self.year,self.name,self.height))
AttributeError: 'RobotThree' object has no attribute 'year'
 
#替换为三代继承二代,初始化函数形参数不同的情况下,t3在调用RobotOne中的robot_info,没有year参数时候会报错,要避免
#原因:robot_info在一代函数里,而由于取就近原则取的二代,导致自相矛盾
#解决:避免 或者重写

推荐阅读