class Person (object):
Count = 0 #类属性
def __init__ (self,name,age): #实例属性
# Self.__name = Name#name is a private property; The private property receive is the Set/get method;
# self.__name can also be used with Self.setname (name) The advantage is that when creating an instance, it is not necessary to re-assign (call the Set method), the initialization can be assigned to the value
self. SetName (name)
Self.age = Age
def run (self):
Print (Self.__name + "Runaway")
def setName (self,name): #注意: A set method of a private property
Self.__name = Name
def getName (self): # A private property a Get method
Return Self.__name
p = person ("123", 12)
# P.setname ("123")
Print (P.getname ())
Attention:
When setting the object properties (__init__ () method): The difference between Self.__name and Self.setname ()
Self.__name means that the SetName method is not filtered when the value is initialized, and is also called when the value is re-assigned.
Self.setname () is called by the SetName method to filter the initialization value.
Python private property Set and get Method 2