The methods defined in a class are divided into four categories: public methods, private methods, static methods, and class methods
Both public and private methods belong to objects, and the names of private methods start with "__"
Each object has its own public and private methods that can access members belonging to the class and object
Public methods are called directly through the object name, and private methods cannot be called directly through the object name. They can only be called through self in methods belonging to the object or externally through special methods supported by python.
class Root:
__total = 0
def __init__(self,v):
self.__value = v
Root.__total += 1
def show(self):
print('self.__value:',self.__value)
print('Root.__total:',Root.__total)
@classmethod
def classShowTotal(cls): #class method print
( cls. __total )
@staticmethod
def staticShowTotal(): #Static method print
( Root. __total )
r = Root(3)
r.classShowTotal() #Call the class method through the object
out :
1
r.staticShowTotal() #Call static methods through objects
out :
1
r.show()
out:
self.__value: 3
Root.__total: 1
rr = Root(5)
Root.classShowTotal() #Call class method by class name
out:
2
Root.staticShowTotal() #Call static method by class name
out:
2
Root.show(r) #Call the method and access the instance member
self. __value : 3
Root. __total : 2