12.04公有,私有属性,析构函数,成员属性

class Role(object):
nationality = "JPANA" #公有属性
def __init__(self,name,role,weapon,lift_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon #成员属性
self.lift_value = lift_value
self.money = money
self.__heart = "Normal" #私有属性

def shot(self):
print("%s is shooting..."% self.name)

def got_shot(self):
print("oh....,I got shot....")
self.__heart = "Die"
def __del__(self):
print("del.....run...") #析构函数


r1 = Role("小王","警察","B22")
r2 = Role("小汪","警犬","B20")

def shot2(self):
print("run my own shot method (out of class)",self.name)


print(r1.nationality)
print(r2.nationality)
Role.nationality = "US"
print(r1.nationality)
print(r2.nationality)
print("change r1 of nationality...")
r1.nationality = "China"
print(r1.nationality)
r1.shot()
r1.shot = shot2 #相当于将shot2代替r1.shot了
r1.shot(r1)
shot2(r1) #在r1实例中,__init__的参数可以传到外部,所以说是公有属性。
posted on 2017-12-05 15:21  绿洲2017  阅读(215)  评论(0编辑  收藏  举报