cs_play

# -*-coding:utf-8-*-
__author__ = "logan.xu"


###构造函数
#class Role:
# n = 123
# # 类变量 比如 n = 123
# name = "我是类name"
#
# # 实例变量
# def __init__(self, name, role, weapon, life_value=100, money=15000):
# # 构造函数
# # 作用是在实例化时,做一些类的初始化的工作
# self.name = name # 实例变量(静态属性),作用域就是实例本身
# self.role = role
# self.weapon = weapon
# self.life_value = life_value
# self.money = money
#
# def shot(self): # 类的方法,功能(动态属性)
# print("shooting...")
#
# def got_shot(self):
# print("ah....,I got shot...")
#
# def buy_gun(self, gun_name):
# print("%s just bought %s" % (self.name, gun_name))
#
#
#print(Role.n)
#
#r1 = Role('Alex', 'police', 'AK47')
## 把一个类变成一个具体对象的过程叫实例化(初始化一个类))
## r1.got_shot()
## r1.buy_gun("B51")
#r1.name = "陈冠希" # 修改r1的名字
#r1.bullet_prove = True # 给角色添加装备防弹衣
## print(r1.n,r1.name,r1.bullet_prove)
#r1.n = "改类变量"
## del r1.weapon
#print("r1:", r1.weapon, r1.n)
#
## Role.n = "小龙女"
#
#r2 = Role('Jack', 'terrorist', 'B22')
## 生成一个角色称为role类的实例
#r2.name = "古天乐" ##修改r2的名字
## print(r2.n,r2.name)
#print("r2:", r2.name, r2.n)
## r1.buy_gun("AK47")
#
#Role.n = "小龙女"
#print(r1.n, r2.n)


# 返回结果为
# ah....,I got shot...
# Alex just bought B51
# Alex just bought AK47
#

###析构函数

class Role:
n = 123
# 类变量 比如 n = 123
name = "我是类name"

# 实例变量
def __init__(self, name, role, weapon, life_value=100, money=15000): # 构造函数
##作用是在实例化时,做一些类的初始化的工作
#self.name = name # 实例变量(静态属性)
self.name = name # 实例变量(静态属性),作用域就是实例本身
self.role = role
self.weapon = weapon
self.__life_value = life_value #内部访问life_value 私有属性
self.money = money

def __del__(self):
pass #print("%s彻底死了。。。。啊!!" %self.name)

def show_status(self):
print("name:%s weapon:%s life_val:%s" %(self.name,self.weapon,self.__life_value))


def shot(self): # 类的方法,功能(动态属性)
print("shooting...")

def got_shot(self):
self.__life_value -= 50
print("%s:ah....,I got shot..."%self.name)

def buy_gun(self, gun_name):
print("%s just bought %s" % (self.name, gun_name))



r1 = Role('Alex', 'police', 'AK47')
# 把一个类变成一个具体对象的过程叫实例化(初始化一个类))
# r1.got_shot()
# r1.buy_gun("B51")
r1.buy_gun("AK47")
r1.got_shot()
print(r1.show_status())


r2 = Role('jack','terrorist','B22')
r2.got_shot()

>>>>>>>>>>>>>>>>>>>>>>>>>.

Alex just bought AK47
Alex:ah....,I got shot...
name:Alex weapon:AK47 life_val:50
None
jack:ah....,I got shot...



posted @ 2018-04-24 17:02  裸奔的小鸵鸟  阅读(611)  评论(0编辑  收藏  举报