一、概述
1.定义类(class Dog(object))--> 实例化(d = Dog())---> 实例对象(d)
2. __init__() 构造函数
3. self.name = name 类的属性、成员变量
4. def say_hi() 类的方法 、动态属性
二、访问类的属性
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 shot(self):
print("%s is shooting..." % self.name)
def got_shot(self):
print("ah...,%s got shot..." % self.name)
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') # 生成一个角色
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84740