python面向对象(2)——继承(4)

私有权限

在日常生活中,某些属性和方法我们不想让他给继承子类
此时我们在属性名或者方法名之前加__

class Gun(object):
	def __init__(self):
		self.__length = 100
		
	def __shoot(self):
		print('bang1')
		
class Wuzi(Gun):
	pass
		

wuzi = Wuzi()

wuzi.__shoot()
print(wuzi.__length)

此时会报错,无法调用方法和属性
注意:如果要获取,一般在工作中选择在类的里面设置get_xx()函数

猜你喜欢

转载自blog.csdn.net/weixin_48445640/article/details/108815046