Python 子类调用父类方法
2014-12-08 17:09
736 查看
Python在继承时,如果子类重写了init()函数,则父类的init()不会被调用,这时如果子类只是想要对父类的init()函数进行简单的扩展的话,是很不方便的。那么有没有比较方便的方法来从子类调用父类呢?
第一种是直接使用父类的类名来直接调用。(Python3.3)
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
Parent.__init__(self)
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
第二种是使用形如super(type, obj).methodName(args)调用,第一个参数表示从哪个类开始找其父类(不包括其自身),第二个参数表示类实例(一般使用self),参数args在父类方法的参数只有self时不写。
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
#Parent.__init__(self)
super(Child,self).__init__()
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
当在类内部使用时,super(type, obj).methodName(args)中的type, obj可以省略不写。
另外,在类外面也可以使用这一函数,比如当子类覆盖了父类的某一方法后,想要通过子类的对象调用父类的这一方法:
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
#Parent.__init__(self)
super().__init__()
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
c.sleep()
super(Child,c).sleep()
第一种是直接使用父类的类名来直接调用。(Python3.3)
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
Parent.__init__(self)
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
第二种是使用形如super(type, obj).methodName(args)调用,第一个参数表示从哪个类开始找其父类(不包括其自身),第二个参数表示类实例(一般使用self),参数args在父类方法的参数只有self时不写。
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
#Parent.__init__(self)
super(Child,self).__init__()
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
当在类内部使用时,super(type, obj).methodName(args)中的type, obj可以省略不写。
另外,在类外面也可以使用这一函数,比如当子类覆盖了父类的某一方法后,想要通过子类的对象调用父类的这一方法:
[python] view
plaincopyprint?
class Parent:
def __init__(self):
print('This is parent init.')
def sleep(self):
print("Parent sleeps.")
class Child(Parent):
def __init__(self):
#Parent.__init__(self)
super().__init__()
print('This is child init.')
def sleep(self):
print("Child sleeps.")
c=Child()
c.sleep()
super(Child,c).sleep()
相关文章推荐
- Python实现子类调用父类的方法
- python子类如何调用父类的__init__方法
- Python3基础 super层层调用父类的__init__方法 子类的__init__覆盖了父类的__init__的解决方法
- [转python 父类可以调用子类的方法
- Python 父类调用子类方法的问题
- python中子类调用父类构造方法
- python子类调用父类的方法
- 【Python】Python中子类怎样调用父类方法
- Python开发基础-Day20继承实现原理、子类调用父类的方法、封装
- python子类调用父类的方法
- Python实现子类调用父类的方法
- Python学习笔记--子类调用父类方法
- python子类调用父类的方法
- python基础之继承实现原理、子类调用父类的方法、封装
- python中子类调用父类方法几点细节(正确使用super)
- python子类调用父类的方法
- python学习笔记:子类怎样调用父类方法
- Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
- Python 父类调用子类方法
- Python实现子类调用父类的方法