#定义父类 ''' class Animals: def __init__(self,name,age): self.name=name self.age=age def move(self): print('移动方式%s'%self.name) def speak(self): print('语言') class Dog(Animals): pass d1=Dog('sd',19) d1.move()#直接调用父类里面的方法 #子类也可以创建属于自己的方法以及属性 有时候会因为子类定义新的方法导致调用父类方法错误 #所以我们需要使用super调用父类元素 #也就是说,先使用super函数把父类方法调出来在他之后,在写子类有关的语句 ''' ''' # 使用super调用父类方法 class Animals: def __init__(self,name,age): self.name=name self.age=age def move(self): print('移动方式%s'%self.name) def speak(self): print('语言') class Dog(Animals): def __init__(self,hobby): #调用父类方法的时候应该穿的参数都传进去 super(Dog,self).__init__('dsd',18)#这个的含义就是把Dog类地self对象传到父类 然后调用父类的方法 self.hobby=hobby def x(self): print('{0}'.format(self.hobby)) d1=Dog('ds')#子类穿参数只要传进去自己想要添加的就
python函数继承
最新推荐文章于 2022-06-30 10:14:25 发布