Python——类的继承

1、子类可以调用父类的方法或属性。

class Parent():
	def hello(self):
		print('正在调用父类方法')

# Child继承Parent
class Child(Parent):
	pass

p=Parent()
p.hello()
c=Child()
c.hello()

运行结果:
正在调用父类方法
正在调用父类方法

2、如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法或属性。

class Parent():
	def hello(self):
		print('正在调用父类方法')

# Child继承Parent
class Child(Parent):
	def hello(self):
		print('正在调用子类方法')

p=Parent()
p.hello()
c=Child()
c.hello()

运行结果:
正在调用父类方法
正在调用子类方法

3、自动覆盖会带来一些问题

class Parent():
	def __init__(self):
		self.name = '老王'
		self.family_property = 1e6
	def hello(self):
		print('正在调用父类方法')

# Child继承Parent
class Child(Parent):
	def __init__(self):
		self.name = '小王'
	def hello(self):
		print('正在调用子类方法')

c=Child()
print(c.name)
print(c.family_property)

运行结果:
小王
Traceback (most recent call last):
File “C:\Users\86177\Desktop\temp.py”, line 17, in
print(c.family_property)
AttributeError: ‘Child’ object has no attribute ‘family_property’

解决办法:再次调用父类中的初始化函数

class Parent():
	def __init__(self):
		self.name = '老王'
		self.family_property = 1e6
	def hello(self):
		print('正在调用父类方法')

# Child继承Parent
class Child(Parent):
	def __init__(self):
		# 再次调用父类中的初始化函数
		Parent.__init__(self)
		self.name = '小王'
	def hello(self):
		print('正在调用子类方法')

c=Child()
print(c.name)
print(c.family_property)

运行结果:
小王
1000000.0

4、多重继承的话,如何解决自动覆盖带来的问题
4.1 直接给定父类名字

class Parent():
	def __init__(self):
		self.name = '老王'
		self.family_property = 1e6
	def hello(self):
		print('正在调用父类方法')

class School():
	def __init__(self):
		self.name = '北京大学'
		self.school_address = '北京'
	def open(self):
		print('开学喽')

# Child继承Parent和School
class Child(Parent,School):
	def __init__(self):
		# 再次调用父类中的初始化函数
		Parent.__init__(self)
		School.__init__(self)
		self.name = '小王'
	def hello(self):
		print('正在调用子类方法')

c=Child()
print(c.name)
print(c.family_property)
c.open()
print(c.school_address)

运行结果:
小王
1000000.0
开学喽
北京

4.2 使用super函数,不用直接给定父类名字。这样做会有一个问题,如果多个父类中有同名的方法,前面父类的方法会覆盖后面父类的方法,所以一般不用多重继承。

class Parent():
	def __init__(self):
		self.name = '老王'
		self.family_property = 1e6
	def hello(self):
		print('正在调用父类方法')

class School():
	def __init__(self):
		self.name = '北京大学'
		self.school_address = '北京'
	def open(self):
		print('开学喽')

# Child继承Parent和School
class Child(Parent,School):
	def __init__(self):
		# 再次调用父类中的初始化函数
		super().__init__()
		self.name = '小王'
	def hello(self):
		print('正在调用子类方法')

c=Child()
print(c.name)
print(c.family_property)
c.open()
print(c.school_address)

运行结果:
小王
1000000.0
开学喽
Traceback (most recent call last):
File “C:\Users\86177\Desktop\temp.py”, line 28, in
print(c.school_address)
AttributeError: ‘Child’ object has no attribute ‘school_address’

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值