python 中super().__init__()

父类

	class Person:
	    def __init__(self, name='Person'):
	        self.name=name
	    def fun(self):
	        print('其他函数')

子类

		class Puple(Person):
		    pass
		class Puple_Init(Person):
		    def __init__(self, age):
		        self.age = age  # 虽然继承了父类属性,但对父类又进行了重写,所以没有name属性。
		class Puple_Super(Person):
		    def __init__(self, name, age):
		        self.age = age           # 重写一个属性
		        super().__init__(name)  # 继承父类的属性

实例化

	p1 = Puple()
	p2 = Puple_Init(10)
	p3 = Puple_Super('pp',12)

测试

在这里插入图片描述

Python3.x 和 Python2.x 的一个区别: Python 3 可以使用直接使用 super().xxx 代替 super(Class, self).xxx :

例如:

	python3 直接写成 : super().__init__()
	python2 必须写成 :super(本类名,self).__init__()

参考:https://blog.csdn.net/a__int__/article/details/104600972

Guess you like

Origin blog.csdn.net/weixin_44885180/article/details/120317734