Python3 object-oriented

Object-oriented: Object-Oriented Programming
Object-Oriented Features: 

Encapsulation Encapsulation
Inheritance inherits from 

Polymorphism

Object-oriented programming: Like process-oriented programming, it is a kind of programming idea 
. Classes are abstract collections of things with the same attributes and behavior methods.
Objects are the role of specific instantiation

classes of classes: to create objects, some attributes are generally defined in the class. And functions, when using a class to create an object, the object will have these attributes and functions defined in the
class. Some attributes and functions in the class are the functions of the object extracted from the common attributes and behaviors of all

objects: Implements function and property calls performed by

  1   -*- coding:utf-8 -*-
   2  #How to declare a class in python: 
  3  # class class group 
  4  # object inherited parent class object base class, top-level parent class 
  5  class People(object):
   6      n =123   #Class variable 
  7      name= ' I am a class variable ' 
  8      n_list= []
   9      #Initialization function, which is automatically called when an object is created 
10      # __init__ built-in function 
11      def  __init__ (self,name,age,phone):
 12          #In this function, you need to assign some attributes that should be owned when the object is created 
13          # Object.Attribute name = attribute value
14          #The self here is equivalent to the object name such as: p1 = People('Zhang San', 22,110) The self here is p1 
15 self.name          = name   #Instance variable (static attribute) The scope is the instance itself 
16          self.age = age   #equivalent to p1.age=age 
17          self.phone = phone
 18  
19      def show(self): #class method, also called class function (dynamic attribute) 
20          #here self is equivalent to the object name such as: p1 .show() The self here is p1, which is equivalent to People.show(p1), whoever calls the method in this class is who. 
21  
22          print (self.name )
 23  
24  print (People.n) #Class variables can be used before instantiation 
25  #Create an object using the People class
26  #Basic format of creating an object: object name = class name () 
27 #When an object is created  , a space will be opened in memory to store the object, which has an address 28 #The object created each time They are all a single individual, and their memory addresses are different. 29 p1 = People( ' Zhang San ' ,22,110) # p1 is also called an instance of People, also called an object 30 p2 = People( ' Li Si ' ,26,120 )
 31 #Memory : The space occupied in the computer when the program is running 32 # 33 of automatic memory management in python print (p1)   # <__main__.People object at 0x0000000002217E80> 34 #Get the attribute value of the object 35 #Format : object.attribute name
 

 
 
 
 
 
36 name = p1.name
 37  print (name)
 38 age = p1.age
 39  print (age)
 40 phone = p1.phone
 41  print (phone)
 42  
43  print ( ' ------------ --- ' ,p1.n,p1.name) #Class   variables and instance variables can be used directly >>--------------- 123 Zhang San 
44                                         #If there is a class variable that is name, then also print the same variable name instance variable, no class variable is found 
45  
46  #Modify the attribute value of the object 
47  #Format : object. attribute name = attribute value 
48 p1.name= ' Li Si ' 
49 print (p1.name)
 50  
51  # The attribute value function used in python 
52  # 1. The object to set the attribute value 2. The attribute name to set 3. The attribute value to set 
53 setattr(p1, ' age ' ,21 )
 54  
55  #You can add new attributes to the object 
56  # Object.Attribute name = attribute value If this attribute does not exist, then add attribute 
57 p1.sex = ' male '   #equivalent to the effect of self.sex=sex in the constructor The same, but only for p1, other objects do not have this attribute 
58  print (p1.sex)
 59  
60 #Delete the attribute of the object  61 #After the attribute of the object is deleted, it can no longer be used 62 #
 
 AttributeError: sss The sss attribute to be deleted does not exist 
63  del   p1.sex
 64  #Function to delete object attributes 65 # 1. Object to be deleted 2. Attribute name to be deleted 66 delattr(p1, ' phone ' ) #or del p1 .phone 67 #After deletion, p1 cannot use this property 68 # print (p1.phone) 69 70 #Change class variable with instance 71 p1.n= ' Change class variable ' 72 #Print with p1 73 print ( ' - --------p1------------ ' ,p1.n)   #
 

 
 
 
 

 
 >>---------p1------------- Change the class variable (equivalent to adding in the memory of p1: p1.n='change the class variable') 
74  #Use p2 print 
75  print ( ' ---------p2----------- ' ,p2.n)   # >>---------p2----- ------ 123 
76  
77 People.n= ' ABCD ' 
78  print ( ' p1: ' ,p1.n, ' p2: ' ,p2.n)   # >>p1: Change class variable p2: ABCD 
79  
80 p1.n_list= ' from p1 ' 
81 p2.n_list= ' from p2 '
82  prints('p1:',p1.n_list,'p2:',p2.n_list,'People:',People.n_list) #>>p1: from p1 p2: from p2 People: []
 83 
 84 # setattr动态添加属性
 85 attr_dict = {'iq':'150','eq':'150','color':'yellow'}
 86 # for遍历
 87 forkey,value in attr_dict.items():
 88      # key is the attribute name, value is the attribute value 
89      # setattr, you can add a string attribute name to the attribute of the object 
90      # p1.key = value This method cannot Adding is equivalent to writing p1.'iq'= '150' 
91      setattr(p1,key,value)
 92  
93  print (p1.iq)
 94  
95  
96  # delattr dynamically deletes attribute 
97  # deletes attribute 98 in object p1
 attr_dict = [ ' iq ' , ' eq ' , ' color ' ]
 99 100 for attr 
 in attr_dict:
101     # 可以使用delattr函数删除
102     delattr(p1,attr)
103 
104 # 属性删除之后,就不能再使用了
105 # print (p1.iq)

 

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293312&siteId=291194637