I. Class body
A class body generally consists of two parts:
Variable part, also called static variable, static field.
Method (function) section.
classPerson :#"Body: two parts: Variable part, method (function) part" "Mind ='have thought' #variables, static variables, static fieldsAnimal ='Advanced Animals'Faith='have faith' def __init__(self,name,age,hobby): Self.name= Name#Person.money = ' using currency 'Self.age =Age Self.hobby=HobbydefWork (self):#methods, functions, dynamic variables Print('%s will work ...'%self.name)defShop (self):Print('human beings can consume ....')Print(person.__dict__)#returns all the contents of the class as a dictionary (no additions or deletions can be made on this basis)
Print (person. __dict__ ['mind']) # The content of key mind in query dictionary form
Two. Related operations in the class
1. At the angle of the class name
1. Enquiry
person.__dict__ all content in the query class (cannot be deleted or modified)
Variable name in person.__dict__[' class] query variable in class
2. The almighty '. ', make additions and deletions to the individual variables in the class
The name of the class. Already has a variable name #查询变量名对应的内容
The class name. New variable name = variable content #增加一个变量
The class name. Existing variable name-new variable content #对类中已有的变量名对应的变量内容进行修改
Del class name. already has a variable name #对类中的变量惊醒删除操作
3. Class names Call methods (functions) in a class
Class name. Method Name () #此处调用方法 (function) need to pass arguments to the function
2. At the angle of the object
This process of the class name + (): The process of instantiating (the process of a common object)
Automatically executes the __init__ method in the class, passing the object space to the self parameter of the __init__.
Encapsulates the appropriate properties for an object.
classPerson :" "class Body: Two parts: Variable part, method (function) part" " Mind='have thought' #variables, static variables, static fieldsAnimal ='Advanced Animals'Faith='have faith' def __init__(self,name,age,hobby): Self.name= Name#Person.money = ' using currency 'Self.age =Age Self.hobby=HobbydefWork (self):#methods, functions, dynamic variables Print('%s will work ...'%self.name)defShop (self):Print('human beings can consume ....') ret= Person ('Alex', 1000,'Oldwomen')#creates an object, and the corresponding propertyPrint(Ret.__dict__)#querying the entire contents of an object
Print (ret.name) # Check
= 175 # Increase
del ret.name # Delete
# Change
3. Manipulating variables in a class at the object's angle (query only)
Object name. Variable Name
4. The object angle invokes a method (function) in the class #一般都是通过对象执行类中的方法
Object name. method (function) name
Python Learning (Face object: Class)