首页 > 其他 > 详细

day⑥:面向对象

时间:2016-02-26 18:44:34      阅读:163      评论:0      收藏:0      [点我收藏+]

一.类初识-cs_game
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. class Role(object): #新式类的写法
  4. ac=None
  5. def __init__(self,name,role,weapon,life_value=100,money=15000):
  6. self.name=name
  7. self.role=role
  8. self.weapon=weapon
  9. self.life_value=life_value
  10. self.money=money
  11. def shot(self):
  12. print("shooting...")
  13. def got_shot(self):
  14. print("ah....,I got shot...")
  15. def buy_gun(self,gun_name):
  16. print("just bought %s"%gun_name)
  17. self.weapon=gun_name
  18. #实例化
  19. t1=Role("yaobin","boy","AK47") #此时self相当于t1,Role(t1,"yaobin","boy","AK47")
  20. t2=Role("test","girl","B22") #此时self相当于t1,Role(t1,"test","girl","B22")
  21. t3=Role("test2","girl","B23")
  22. t4=Role("test3","girl","B24")
  23. #执行方法
  24. #t1.buy_gun("new1") #相当于Role.buy_gun(t1,"new1")
  25. #t2.buy_gun("new2") #相当于Role.buy_gun(t2,"new2")
  26. #print(t1.weapon)
  27. #print(t2.weapon)
  28. t1.ac="China Brand" #实例改属性
  29. t2.ac="US Brand" #实例改属性
  30. Role.ac="Janpanese Brand" #改类属性
  31. print("t1:",t1.weapon,t1.ac)
  32. print("t2:",t2.weapon,t2.ac)
  33. print("t3:",t3.weapon,t3.ac)
  34. print("t4:",t4.weapon,t4.ac)
  35. # print(Role.buy_gun)
  36. #结果:
  37. t1: AK47 China Brand
  38. t2: B22 US Brand
  39. t3: B23 Janpanese Brand
  40. t4: B24 Janpanese Brand


二.继承_school_class
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. class SchoolMember(object):
  4. members=0
  5. def __init__(self,name,age,sex):
  6. self.name=name
  7. self.age=age
  8. self.sex=sex
  9. self.enroll()
  10. def tell(self):
  11. print("my name is %s" %self.name)
  12. def enroll(self):
  13. SchoolMember.members+=1
  14. print("\033[32;1mnew member [%s] is enrolled,now there are [%s] members.\033[0m"%(self.name,SchoolMember.members) )
  15. #def __del__(self):
  16. # print("\033[31;1mmember [%s] is dead!\033[0m" %self.name)
  17. class Teacher(SchoolMember):
  18. def __init__(self,name,age,sex,course,salary):
  19. super(Teacher,self).__init__(name,age,sex)
  20. #SchoolMember.__init__(self,name,age,sex) #经典类的写法,旧,不要用
  21. self.course=course
  22. self.salary=salary
  23. def teaching(self):
  24. print("Teacher [%s] is teaching [%s] for class [%s]"%(self.name,self.course,"s12"))
  25. class Student(SchoolMember):
  26. def __init__(self,name,age,sex,course,tuition):
  27. super(Student,self).__init__(name,age,sex)
  28. self.course=course
  29. self.tuition=tuition
  30. def pay_tution(self):
  31. print("cao ,student [%s] paying tution" %self.tuition)
  32. s1=Student("yaobin",24,"boy","py","1800")
  33. t1=Teacher("alex",30,"boy","py","100000")
  34. s2=Student("meimei",25,"girl","py","1800")
  35. t2=Teacher("wusir",27,"boy","py","120000")
  36. s1.tell()
  37. s1.pay_tution()
  38. t1.tell()
  39. t1.teaching()
  40. #结果:
  41. new member [yaobin] is enrolled,now there are [1] members.
  42. new member [alex] is enrolled,now there are [2] members.
  43. new member [meimei] is enrolled,now there are [3] members.
  44. new member [wusir] is enrolled,now there are [4] members.
  45. my name is yaobin
  46. cao ,student [1800] paying tution
  47. my name is alex
  48. Teacher [alex] is teaching [py] for class [s12]






day⑥:面向对象

原文:http://www.cnblogs.com/binhy0428/p/5221462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!