First, meaning
Everything in Python is an object, and classes and types in Python3 (common data types such as list dictionaries, etc.) are a concept, and types are classes.
Second, examples
#! /usr/bin/env Python3#-*-coding:utf-8-*-Print(Type ([1,23,4]))#<class ' list ' >Print(list)classSchool_student:job='Student' defStudy (self):Print('studying hard') defRelax (self):Print('realxing Happy')Print(school_student)#<class ' __main__. School_student ' >#This shows that data types such as lists are also classes, which are combinations of features and skillsLi= [[+]#definition list (instantiation)Li2 =[]li2.append (0)#li.append () and Li2.append () call the same method, but the bound object is different, the binding method is differentLi.append (4)#adding elements [1, 2, 3, 4],append equivalent to the built-in method of the list classList.append (li,5)#[1, 2, 3, 4, 5] # the same as Li.append (4)Print(LI)
Another example:
#type dict is Class Dict>>>List<class 'List'>#instantiated to 3 objects L1,l2,l3>>> l1=list ()>>> l2=list ()>>> l3=list ()#three objects have a binding method append, which is the same function, but the memory address is different>>>L1.append<built-inchMethod Append of List object at 0x10b482b48>>>>L2.append<built-inchMethod Append of List object at 0x10b482b88>>>>L3.append<built-inchMethod Append of List object at 0x10b482bc8>
#操作绑定方法l1.append(3),就是在往l1添加3,绝对不会将3添加到l2或l3
>>> l1.append(3) # [3]
#调用类list.append(l3,111)等同于l3.append(111)>>> list.append(l3,111) #等同于l3.append(111) [111]
Third, object-oriented practice
1, write a class, mass production objects, and statistics of the number of objects produced
#! /usr/bin/env Python3#-*-coding:utf-8-*-classStudents:country=' China' #The data attributes are commonSchool ='HelloWorld'Count=0def __init__(self,name,age,sex,major):#function Properties, which are used for objectsSelf.name =name Self.age=Age Self.sex=Sex self.major=Major Students.count+ = 1#each time the __init__ method is triggered, an object is generated, the counter is incremented once, and its data properties are changed defStudy (self):Print('Study Everyday') defRelax (self):Print('Relaxing') STU1= Students ('Ting Zi', 19,'female','Comic')Print(Stu1.count)#1STU2 = Students ('Grandpa', 36,'male','Direct')Print(Stu2.count)#2STU3 = Students ('The star of Lucerne'21st'male','Detective')Print(Students.count)#3Print(Stu1.count)#3Print(Students.__dict__)Print(STU1.__dict__)#{' name ': ' Ting zi ', ' age ': +, ' sex ': ' female ', ' major ': ' Comic '}Print(STU2.__dict__)View Code
2, imitate lol to write two heroes, heroes have nicknames, attack, health and other attributes; instantiate two heroic objects;
Heroes must have a fight between the beaten side will Drop blood, blood volume of less than 0 to determine death.
#! /usr/bin/env Python3#-*-coding:utf-8-*-classGaylen:camp='Desmarcia' def __init__(self,nickname,aggressivity,life_valid): Self.nickname=Nickname Self.aggressivity=aggressivity Self.life_valid=Life_validdefAttack (self,target): Target.life_valid-=self.aggressivityifTarget.life_valid <=0:Print('%s is died'%target.nickname)classHand_of_knoxus:camp='Desmarcia' def __init__(self,nickname,aggressivity,life_valid): Self.nickname=Nickname Self.aggressivity=aggressivity Self.life_valid=Life_validdefAttack (self,target): Target.life_valid-=self.aggressivityifTarget.life_valid <=0:Print('%s is died'%target.nickname) Herol= Gaylen ('Gues Gaillen', 60,300) Hero2= Hand_of_knoxus ('Connaught Hand', 70,280) Herol.attack (Hero2) hero2.attack (Herol) hero2.attack (Herol) hero2.attack (Herol) hero2.attack (Herol) Hero2.attack (Herol)Print(herol.life_valid)Print(Hero2.life_valid)View Code
Python all objects