首页 > 代码库 > python学习day06
python学习day06
class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only raise NotImplementedError("Subclass must implement abstract method") class Cat(Animal): def talk(self): return ‘Meow!‘ class Dog(Animal): def talk(self): return ‘Woof! Woof!‘ animals = [Cat(‘Missy‘), Dog(‘Lassie‘)] for animal in animals: print animal.name + ‘: ‘ + animal.talk()
#!_*_coding:utf-8_*_#__author__:"Alex Li" class SchoolMember(object): members = 0 #初始学校人数为0 def __init__(self,name,age): self.name = name self.age = age def tell(self): pass def enroll(self): ‘‘‘注册‘‘‘ SchoolMember.members +=1 print("\033[32;1mnew member [%s] is enrolled,now there are [%s] members.\033[0m " %(self.name,SchoolMember.members)) def __del__(self): ‘‘‘析构方法‘‘‘ print("\033[31;1mmember [%s] is dead!\033[0m" %self.name)class Teacher(SchoolMember): def __init__(self,name,age,course,salary): super(Teacher,self).__init__(name,age) self.course = course self.salary = salary self.enroll() def teaching(self): ‘‘‘讲课方法‘‘‘ print("Teacher [%s] is teaching [%s] for class [%s]" %(self.name,self.course,‘s12‘)) def tell(self): ‘‘‘自我介绍方法‘‘‘ msg = ‘‘‘Hi, my name is [%s], works for [%s] as a [%s] teacher !‘‘‘ %(self.name,‘Oldboy‘, self.course) print(msg) class Student(SchoolMember): def __init__(self, name,age,grade,sid): super(Student,self).__init__(name,age) self.grade = grade self.sid = sid self.enroll() def tell(self): ‘‘‘自我介绍方法‘‘‘ msg = ‘‘‘Hi, my name is [%s], I‘m studying [%s] in [%s]!‘‘‘ %(self.name, self.grade,‘Oldboy‘) print(msg) if __name__ == ‘__main__‘: t1 = Teacher("Alex",22,‘Python‘,20000) t2 = Teacher("TengLan",29,‘Linux‘,3000) s1 = Student("Qinghua", 24,"Python S12",1483) s2 = Student("SanJiang", 26,"Python S12",1484) t1.teaching() t2.teaching() t1.tell()
class Role(object): def __init__(self,name,role,weapon,life_value=http://www.mamicode.com/100,money=15000): self.name = name self.role = role self.weapon = weapon self.life_value = life_value self.money = money def shot(self): print("shooting...") def got_shot(self): print("ah...,I got shot...") def buy_gun(self,gun_name): print("just bought %s" %gun_name) r1 = Role(‘Alex‘,‘police‘,‘AK47’) #生成一个角色r2 = Role(‘Jack‘,‘terrorist‘,‘B22’) #生成一个角色
python学习day06
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。