I. Overview
In the previous blog we have already talked about the class of knowledge, we come to review the following:
- Instantiate object (d = Dog ()), instantiation (d), definition Class (object)
- __init__ () constructor
- Self.name = name is called a property, member variable, field
- def sayhi (self) is called method, dynamic property
Let's continue to learn the characteristics of the class.
second, Access class properties
2.1 Member variables and parameters
Class Dog (object): "Dog class" def __init__ (self,name): self.name = name #类的成员变量 def sayhi (self) : print ("The dog {0} is Sayhi". Format (Self.name)) def Eat (Self,food): #传入一个参数, Parameter name: Food print ("the Dog name is {0},it. Format (self.name,food)) d = Dog ("Alex") #实例化对象d. Sayhi () #调sayhi方法d. Eat ("Hotdog") #调eat方法 #输出the dog Alex is sayhithe dog name was alex,it like food is hotdog
① a lot of people have doubts, why the Eat method inside will pass a food parameter name it? And why can't this be used in any other way? And self.name can do it?
Because food it is just a parameter of the Eat method, not a member variable of a class, only used within its own method, and Self.name is a member variable of the class, it is the property of the class, so it can be called.
② Why is the first argument of each method of a class self?
Because the class is instantiated, the instance object itself needs to be passed in, so that it can invoke the properties and methods of the object itself.
2.2 Modifying the properties of an object
Class Dog (object): "Dog class" def __init__ (self,name,age): self.name = name Self.age = Age def Sayhi (self,update_age): #传入一个update_age参数 self.age = update_age #修改对象的age属性 d = Dog ("Alex") D.sayhi ( #第1次在类的方法中修改print (d.age) d.age = #第2次在类外面修改print (d.age) #输出2219
Note: You can modify the properties of a class in a method of a class or outside a class
third, define private properties
Note: We have previously said that we can access the properties of the class, or we can modify the properties of the class at will, but once the class attributes are defined as private, they cannot be accessed and cannot be arbitrarily changed.
3.1 Defining private properties
Class Dog (object): "Dog class" def __init__ (self,name,age): self.name = name Self.age = Age self . __sex = "Man" #定义私有属性__sex def sayhi (self,update_age): self.age = update_age D = Dog ("Alex") print (d.__ Sex) #访问私有属性__sex #输出Traceback (most recent): File "e:/pycharmprojects/pythontest/day6/package. Py", line +, in <module> Print (d.__sex) attributeerror: ' Dog ' object has no attribute ' __sex ' #报错, said no __sex this attribute
As you can see from the example above, private properties are not accessible and cannot be modified. But we want to visit, but not to change, what do we do?
3.2 Get method Access private property
Although we cannot access private properties externally, we have access to private properties within the class, so we use the following method to access the private properties
Class Dog (object): "Dog class" def __init__ (self,name): self.name = name self.__sex = "Man" # Define private properties Sex def get_sex (self): #定义get方法 return self.__sex #返回私有属性sex值 d = Dog ("Alex") D_sex = D.get_sex ( ) #调用实例对象d的get_sex () method gets the private property Sexprint (D_sex) #输出man
Note: This method can only access private properties, but cannot change private properties
3.3 Mandatory access to private properties
The above method can only be accessed, but cannot be modified, the following method is more violent, can be accessed or modified, that is: object name. _ Class Name __ Property name
Class Dog (object): "Dog class" def __init__ (self,name): self.name = name self.__sex = "Man" #定义私有方法sex d = Dog ("Alex") print (d._dog__sex) #访问私有属性sexd. _dog__sex = "Woman" #修改私有属性sexprint (D._dog__sex) #打印修改后的值 # Output Manwoman
Iv. Summary
- Define private properties: Self.__private_attr_name = Private_attr_name
- Enforce access to private properties: Object name. _ Class Name __ Property name (D._dog__sex)
- Provides read-only interface access to external:
def get_sex (self):
Return Self.__sex
Features like Python basic 8 are explained