class 的三大特性
封装:内部调用对于外部用户是透明的
继承: 在分类里的属性,方法被自动继承
多态:调用这个功能,可以使多个类同时执行
r1 = Role(r1, 'Alex', 'Police', '15000') #实际上把r1赋给了self
self.name = 'Alex' # r1.name = 'Alex'
self.role = 'Police' # r1.role = 'Police'
self.money = 15000 # r1.money = 15000
r1.buy_guy() # Role.buy_guy(r1)
第一个例子 (建立一个简单的类)
class Dog:
def __init__(self, name):
self.name = name
def bulk(self):
print(‘%s : wa wa’%self.name)
dog = Dog('xiaoming')
dog.bulk()
第二个例子 #建立游戏角色 (封装) 私有属性,私有函数
class Role(object):
n = 123 #类变量
name = 123
def __init__(self, name, role, weapon, life_value = 100, money = 20000) :
self.name = name #实例变量
self.role = role
self.weapon = weapon
self.__life_value = life_value #函数的私有属性,外部不可以更改,只限于内部调用,可以打印出来
self.money = money
def __shot(self): # 私有函数,外部不可调用
print('shoting')
def got_shot(self):
self.__life_value -= 50
def buy_gun(self):
print('%s buy %s'%(self.name, self.weapon)
def show_status(self):
print('name : %s weapon : %s life_value %s‘%(self.name, self.weapon, self.__life_value))
r1 = Role('alex', 'Police', 'AK47') # 创建Role的实例
r1.name = 'xiaoming' #更改实例变量的name
del r1.name #删除实例变量的name
del r1 #删除实例r1
print(r1.n) #输出结果123 实例中没有该变量就去找类变量
print(r1.name) #输出结果 alex 实例中有该变量就用实例中的变量
r1.got_shot()
r1.show_status()
第三个例子,创建学校 (继承)
class School(object):
def __init__(self, name, addr):
self.name = name
self.addr = addr
self.student = []
self.teacher = []
def enroll(self, stu): #招收学生
print('you have been enrolled')
self.student.append(stu)
def hire(self, teac): #招收老师
print('you have been hired')
self.teacher.append(teac)
class SchoolMember(object):
def __init__(self, name, age):
self.name = name
self.age = age
def tell():
pass
class Student(SchoolMember): #继承SchoolMember
def __init__(self, name, age, stu_id, grade): #继承的函数,其变量也需要写在里面
super(Student, self). __init__(name, age) #=SchoolMember.__init__(name, age)
self.name = name
self.age = age
self.stu_id = stu_id
self.grade = grade
def tell(self):
print('''
name : %s
age : %s
stu_id : %s
grade : %s'''%(self.name, self.age, self.stu_id, self.grade))
def payment(self, number):
print('%s need to pay %s money'%(self.name, number))
class Teacher(SchoolMember):
def __init__(self, name, age, salary, course):
super(Teacher, self) .__init__(name, age)
self.name = name
self.age = age
self.salary = salary
self.course = course
def tell(self):
print('''
name : %s
age : %s
salary : %s
course : %s'''%(self.name, self.age, self.salary, self.course))
def teaching(self):
print('%s is teaching %s'%(self.name, self.course))
stu1 = Student('成华', 23, 10001, 1000) #创建学生的实例
tea1 = Teacher('小明', 22, 20000, 'python') #创建老师的实例
stu1.tell()
stu1.payment(20000)
sc1 = School('oldboy', '沙河') #创建学校1
sc2 = School('幼儿园', '天河') #创建学校2
sc1.enroll(stu1) #把stu1实例存储在sc1.student里面,可以实现sc1 与 stu1 的关联
sc2.hire(tea1) #把tea1实例存储在sc1.teacher里面,可以实现sc2 与 tea1 的关联
print(sc1.student[0].name) # 等价于 stu1.name
print(sc2.teacher[0].name) #等价于 tea1.name
第四个例子(多态) 一个接口多种实例形态
#通过animal_talk一个接口 可以实现多种动物的叫声
class Animal(object):
def __init__(self, name):
self.name = name
def eat(self):
print('%s is eating'%self.name)
def sleep(self):
print('%s is sleeping'%self.name)
@staticmethod #不需要创建实例
def animal_talk(obj): #实例作为参数
obj.talk() #如果传入cat,等价于调用了cat.talk()
class Cat(object):
def __init__(self, name):
self.name = name
def talk(self): #相同的函数名
print('%s miaomiao'%self.name)
class Dog(object):
def __init__(self, name):
self.name = name
def talk(self): #相同的函数名
print('%s wawa'%self.name)
cat = Cat('淑华')
dog = Dog('小花')
Animal.animal_talk(cat) #cat为传入的类
Animal.animal_talk(dog) #dog为传入的类