I. Overview
1. Define class (object) and instantiate (d = Dog ())---> Instance object (d)
2. __init__ () constructor
3. Self.name = property of the name class, member variable
4. Def say_hi () class methods, dynamic Properties
Second, access the properties of the class
Class Role (object): def __init__ (self, name, Role, weapon, life_value=100, money=15000): self.name = name Self.role = role Self.weapon = weapon self.life_value = life_value Self.money = Money def shot (self): C9/>print ("%s is shooting ..."% self.name) def got_shot (self): print ("ah...,%s got shot ..."% self.name) def buy_gun (self, gun_name): print ("%s just bought%s"% (self.name,gun_name)) r1 = Role (' Alex ', ' police ', ' AK4 7 ') # generates a role r2 = role (' Jack ', ' terrorist ', ' B22 ') # generates a role
2.1 Accessing the properties and methods of a class
We use an instance object . Properties/Methods to access
R1.shot () # Call the Shot method R2.buy_gun (' B13 ') # call Buy_gun method and pass the parameter print (r1.role) # Print the properties of the class # output Alex is shooting ... Jack just bought B13police
2.1 Modifying the properties of an object
In the example above we have replaced the weapon for R2, which is B22--and B13. But in fact we call R2 the character's weapon attribute is, will find his weapon has not changed:
R2.buy_gun (' B13 ') print (r2.weapon) # output jack just bought B13b22 # you can see the weapons are still B22
In fact, we can change the properties of the object directly in the purchase of the weapon:
def buy_gun (self, gun_name): print ("%s just bought%s"% (Self.name, gun_name)) self.weapon = gun_name # in Square In-Law change properties R2.buy_gun (' B13 ') print (r2.weapon) #输出Jack just bought B13b13 # you can see the weapons have changed
Third, private properties
3.1. Define private properties
Once a property of a class is defined as a private property, it cannot be externally invoked or arbitrarily modified. Private properties can only be used internally by the class.
class Person (object): def __init__ (self, name, job, phone, address): self.name = name self.job = Job Self.phone = Phone self.__address = Address # defines a private property def sayhi (self): print ("hell,%s"% self.name) P1 = Person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') print (p1.name) print (p1.__address) # Access private Property # output Bigberg File "G : Private property of the/python/untitled/study6/class. py ", line <module> print (p1.__address) attributeerror: ' Person ' object has no attribute ' __address '
The result of the run is that the Access property name can be passed, but the direct access to the private property self.__address an error. However, if it is done by other means, it can be accessed.
3.2. Get methods to access private properties
The external cannot directly access the private property, but it can be accessed inside the class, so we can define a method to get the private property.
class Person (object): def __init__ (self, name, job, phone, address): self.name = name self.job = Job Self.phone = Phone self.__address = Address def get_private (self): return self.__address def sayhi ( Self): print ("hell,%s"% self.name) P1 = person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') res = P1.get_private () print (res ) # Output Hz
3.3 Mandatory access to private properties
We also have a way to enforce access to private properties and even to modify the value of a private property. Method: Object Name. _ Class Name __ Property name.
class Person (object): def __init__ (self, name, job, phone, address): self.name = name self.job = Job Self.phone = Phone self.__address = Address def get_private (self): return self.__address def sayhi ( Self): print ("hell,%s"% self.name) P1 = person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') print (p1._person__address) # Access private properties Print ("----change----") p1._person__address = ' BJ ' # Modify private properties Print (p1._person__address) # Output Hz----change----BJ
Classes are an important tool for simplifying and optimizing applications.
1. Inheritance: The ability of subclasses to inherit the characteristics of a parent class. It embodies and expands the sharing of object-oriented programming method, which makes the same type object share data and program code, and improves the reusability of the program. The parent class is a class that can further define the derived new class, which is the starting point of the other class and the new class that is created by a more specific feature definition.
2. Polymorphism: Some associated classes contain a method program with the same name, but the contents of a method program can be different. The invocation of which is determined by the class of the object at run time can lead to different actions when the same message is received by different objects.
3. Abstraction: Extracts a characteristic of a class or object that is unique, without processing the class or other information of the object.
Iv. Summary
1. Define private properties: Self.__private_attr_name = Private_attr_name
2. Enforce access to private properties: Object name. _ Class Name __ Property name (D._dog__sex)
3. Provide read-only interface access to external:
def get_sex (self):
Return Self.__sex