Python object-oriented (2)-inheritance (4)

Private permissions

In daily life, we don’t want him to inherit subclasses for certain attributes and methods .
At this time, we add __ before the attribute name or method name.

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)

At this time, an error will be reported and methods and attributes cannot be called.
Note: If you want to get it, you usually choose to set the get_xx() function in the class in your work.

Guess you like

Origin blog.csdn.net/weixin_48445640/article/details/108815046