python 内置函数

outwater 2020-07-05

class Role(object):    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 __del__(self):#析构函数,在实例释放、销毁的时候自动调用,通用用于做一些收尾工作        print(‘%s 彻底嗝屁了‘%self.name)    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))r1 = Role(‘Alex‘, ‘police‘, ‘AK47‘) #实例化(初始化一个类,造一个对象)r2 = Role(‘Jack‘, ‘terrorist‘, ‘B22‘)  #生成一个角色r1.buy_gun(‘AK47‘)

相关推荐