class Class_1():
def __init__(self):
self.var_1 = '父属性' # *
self.var_2 = '父属性'
self.fun_1() # 第一次输出
def fun_1(self):
print('父方法')
print(self.var_1)
print(self.var_2)
class Class_2(Class_1):
def __init__(self):
super().__init__() # 调用父类的构造方法
self.var_1 = '子属性'
self.fun_1() # 第二次输出
self.var_2 = '子属性'
def fun_1(self):
print('子方法')
print(self.var_1) # *如果置于实例方法之后,会引发错误 AttributeError: 'Class_2' object has no attribute 'var_1'
print(self.var_2)
obj_test = Class_2()
print(r'''===解释===
子方法 调用子类的构造方法(11),调用父类的构造方法(12),调用行(3-5),同名实例方法被子类(16)覆盖
父属性 引用行(3)
父属性 引用行(4)
子方法 调用行(13),调用实例方法(14),其中同名实例属性被子类(13)覆盖,调用行(15)
子属性 引用行(13)
父属性 引用行(4)
==========''')
结论
同名实例函数被覆盖
实例变量则根据实际运行顺序而定
更新
https://cloud.tencent.com/developer/article/1915788
1
class Class_1():
def fun_1():
print('父方法')
class Class_2(Class_1): # 继承父类
# fun_1() # NameError: name 'fun_1' is not defined
def fun_1():
print('子方法')
Class_1.fun_1() # 指明调用父类的方法
fun_1() # 若无指明,默认调用子类的方法
obj_test = Class_2()
obj_test.fun_1() # TypeError: Class_2.fun_1() takes 0 positional arguments but 1 was given
2
class Class_1():
def __init__(self):
pass
def fun_1(self):
print('父方法')
class Class_2(Class_1): # 继承父类
# fun_1() # NameError: name 'fun_1' is not defined 若无super()(继承父类的方法), 代码块Class_2本身没有继承父类的方法
def fun_1():
print('子方法')
fun_1() # 1
def fun_1(self): # 2
print('子方法')
# self.fun_1() # NameError: name 'self' is not defined
obj_test = Class_2()
obj_test.fun_1()
print(r'''
子方法 # 1
子方法 # 2
''')
3
在Python中,super()
函数用于调用父类的方法(不是属性!)。它通常在子类的方法中使用,以便在子类中扩展或重写父类的方法时,仍然能够调用父类的实现。
super()
函数的一般语法是:
super().method_name(arguments)
这里的method_name
是父类中的方法名,arguments
是传递给父类方法的参数。
super()
函数的调用会返回一个代理对象,该对象会自动将方法调用委托给父类。这样,你就可以在子类中扩展父类的方法,而不必重写整个方法。
下面是一个示例,展示了如何在子类中使用super()
函数调用父类的方法:
class ParentClass:
def __init__(self, name):
self.name = name
def say_hello(self):
print(f"Hello, {self.name}!")
class ChildClass(ParentClass):
def __init__(self, name, age):
super().__init__(name)
self.age = age
def say_hello(self):
super().say_hello()
print(f"I am {self.age} years old.")
child = ChildClass("Alice", 10)
child.say_hello()
输出结果为:
Hello, Alice!
I am 10 years old.
在这个示例中,ChildClass
继承自ParentClass
。在子类的构造函数中,我们使用super().__init__(name)
调用了父类的构造函数,以便初始化父类的属性。在子类的say_hello
方法中,我们使用super().say_hello()
调用了父类的say_hello
方法,然后在子类中添加了额外的打印语句。
这样,通过使用super()
函数,我们可以在子类中扩展父类的方法,而不必重写整个方法的实现。
4
在Python中,要访问父类的属性,可以使用以下两种方式:
- 使用
super()
函数结合属性名来访问父类的属性。在子类中,可以通过super().属性名
来访问父类的属性。例如:
class ParentClass:
def __init__(self):
self.property = "Parent Property"
class ChildClass(ParentClass):
def __init__(self):
super().__init__()
self.property = "Child Property"
def print_properties(self):
print(super().property) # 访问父类的属性
print(self.property) # 访问子类的属性
child = ChildClass()
child.print_properties()
输出结果为:
Parent Property
Child Property
在这个示例中,ChildClass
继承自ParentClass
。在子类的print_properties
方法中,我们使用super().property
来访问父类的属性,使用self.property
来访问子类的属性。
- 直接通过父类名来访问父类的属性。在子类中,可以通过
父类名.属性名
来访问父类的属性。例如:
class ParentClass:
def __init__(self):
self.property = "Parent Property"
class ChildClass(ParentClass):
def __init__(self):
super().__init__()
self.property = "Child Property"
def print_properties(self):
print(ParentClass.property) # 访问父类的属性
print(self.property) # 访问子类的属性
child = ChildClass()
child.print_properties()
输出结果与上面的示例相同:
Parent Property
Child Property
在这个示例中,我们直接使用ParentClass.property
来访问父类的属性,使用self.property
来访问子类的属性。
无论是使用super().属性名
还是父类名.属性名
,都可以访问父类的属性。选择哪种方式取决于具体的情况和个人偏好。