Learning Points of knowledge:
Class
constructor function
Destructors
Private methods, private properties
class variables
Instance variable
1 #__*__ coding:utf-8 __*__2 __author__="david.z"3 4 classRole:5n = 123#class Variables6n_list=[]7Name ="I am the class name"8 def __init__(self,name,role,weapon,life_value=100,money=15000):9 #constructor FunctionTen #do some initialization of classes at instantiation One ASelf.name = Name#r1.name=name instance variable (static property), scope is the instance itself -Self.role =role -Self.weapon =Weapon theSelf.__life_value=Life_value -Self.money = Money - def __del__(self):# Destructors - Print("%s was completely dead ... "%self.name) + defShow_status (self): - Print("name:%s weapon:%s life_value:%s"% (self.name,self.weapon,self.__life_value)) + defShot (self): A Print("Shooting ...") at defGot_shot (self): -Self.__life_value-= 50 - Print("%s:ah...,i got shot ..."%self.name) - - defBuy_gun (self,gun_name): - Print("%s just bought%s"%(self.name,gun_name)) inR1 = Role ('Chenronghua','Police','AK47') -R1.buy_gun ("AK47") to r1.got_shot () + - Print(R1.show_status ()) the *r2 = Role ('Jack','Terrorist','B22') $R2.got_shot ()
Python uses classes to build roles in CS games