标签:python class
1.类的定义
class Role(object): def __init__(self,name,role,weapon,life_value): self.name = name self.role = role self.weapon = weapon self.life_val = life_value def buy_weapon(self,weapon): print("%s is buying [%s]" %(self.name,weapon)) self.weapon = weapon p1 = Role("san","Police","B10",90) t1 = Role("liwen","Terrorist","B11",100) p1.buy_weapon("AK47") t1.buy_weapon("B51") print("P1:",p1.weapon) print("T1:",t1.weapon)
2.类的继承
#!/usr/bin/env python #-*- coding:utf-8 –*- class SchoolMember(object): def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def enroll(self): print("SchoolMember [%s] is enrolled!" %self.name) class Teacher(SchoolMember): def __init__(self,name,age,sex,course,salary): super(Teacher,self).__init__(name,age,sex) self.course = course self.salary = salary def teaching(self): print("Teacher [%s] is teaching [%s]" %(self.name,self.course)) class Student(SchoolMember): def __init__(self,name,age,sex,course,tution): super(Student,self).__init__(name,age,sex) self.course = course self.tution = tution def pay_tution(self): print("cao,student [%s] paying tution [%s]" %(self.name,self.tution)) #t1 = Teacher("alex",21,"F","PY",1000) s1 = Student("san",24,"M","PY",500)
本文出自 “AMY” 博客,请务必保留此出处http://amy2015.blog.51cto.com/11407620/1773513
标签:python class
原文地址:http://amy2015.blog.51cto.com/11407620/1773513