#!usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = "Samson"
# # #类变量与实例变量
Class Role:
n = 123# class variable, stored in the class, not stored in the instance, can be used to save space
def __init__ (self, name, role, weapon, Life_value = +, Money = 15000):
#构造函数
#在实例化时做一些类的初始化工作
Self.name = name# instance variable (static property), stored in class
Self.role = Role
Self.weapon = Weapon
Self.life_value = Life_value
Self.__money = money# Private property, accessible only within the class
Def shot (self): #类的方法, features (dynamic properties)
Print ("Shooting ...")
def got_shot (self):
Print ("%s:ah...,i has got shot ..."%self.name)
def __show_money (self): #类的私有函数, can only be accessed within the class
Print ("money:%s"% Self.__money)
def buy_gun (self, gun_name):
Print ("%s just bought%s"% (Self.name, gun_name))
def __del__ (self): #析构函数, executed when an instance is released, destroyed, and usually used to do some finishing work, such as closing some databases, closing some open files
Print ("Execution destructor")
Print (Role)
R1 = Role ("Samson", "Police", "AK47") #把一个类变成一个具体对象的过程叫实例化
R1.got_shot () #Role. Got_shot (R1)
R1.bullet_prove = True #给类添加属性, this is possible
r2 = Role ("R2", "police", "AK47") #把一个类变成一个具体对象的过程叫实例化
R2.got_shot () #Role. Got_shot (R1)
Print (R1.weapon)
Del r1.weapon# Delete Properties
R1.N = 145# is equivalent to a new instance variable in R1 without affecting the class variable
Print (R1.N, R2.N) #r2中的n值不变
(not to be continued)
Python: Object-oriented (class)