python- face objects - inheritance

Object-Oriented

Private properties and methods

Private properties and methods obtained by adding __ before the properties and method names.
Private property and privacy methods are the object, not by the outside world and subclasses to directly access

In the outside world can not have access to private properties and methods (not directly print print)
can access private property within the object methods
can subclass object public methods of the parent class indirect access to private property or private method
eg:

class A:
	def __init__(self):
		self.num1 = 11
		#定义私有属性
		self.__num2 = 22
	#定义私有方法
	def __test(self):
		print("私有方法,私有属性:%d %d"% (self.num1,self.__num2))
	#定义公有属性
	def test(self):
		print("父类的公有方法%d"% self.__num2)
class B(A):
	def demo(self):
		#尝试在子类中调用私有属性
		print("self.__num2")
		#尝试在父类中调用私有方法
		self.__test()
b = B()
print(b)
#通过父类的公有方法间接访问到私有属性(self.__num2)或私有方法
b.test()

inherit

1. Method covering (override method) parent class: the same method in the subclass defined in the parent class overrides the parent class
2. The expansion of the parent class: to override the parent method: using super ( ). the method to invoke superclass method original package. For example:. Super () bark ()

Complement: You can only define attributes in the __init__ method!
Note: The summary from Dark Horse class foundation python programming

Guess you like

Origin blog.csdn.net/guaixi/article/details/88636304