Python Learning Day06

Source: Internet
Author: User

classAnimal:def __init__(self, name):#Constructor of the classSelf.name =namedefTalk (self):#Abstract method, defined by convention only        RaiseNotimplementederror ("subclass must implement abstract method") classCat (Animal):defTalk (self):return 'meow!' classDog (Animal):defTalk (self):return 'woof! woof!'Animals= [Cat ('Missy'), Dog ('Lassie')]  forAnimalinchAnimals:PrintAnimal.name +': '+ Animal.talk ()
#!_*_coding:utf-8_*_#__author__: "Alex Li"   classSchoolmember (object): members= 0#the initial number of schools is 0    def __init__(self,name,age): Self.name=name Self.age= AgedefTell (self):Pass     defEnroll (self):" "Register" "schoolmember.members+=1Print("\033[32;1mnew member [%s] is enrolled,now there be [%s] members.\033[0m"%(self.name,schoolmember.members))def __del__(self):" "destructor Method" "        Print("\033[31;1mmember [%s] is dead!\033[0m"%self.name)classTeacher (schoolmember):def __init__(self,name,age,course,salary): Super (teacher,self).__init__(name,age) Self.course=Course Self.salary=Salary Self.enroll ()defTeaching (self):" "Lecture Methods" "        Print("Teacher [%s] is teaching [%s] for class [%s]"% (Self.name,self.course,'S12'))     defTell (self):" "Self Introduction Method" "msg=" "Hi, my name is [%s], works for [%s] as a [%s] teacher!" "% (Self.name,'Oldboy', Self.course)Print(msg)classStudent (schoolmember):def __init__(self, Name,age,grade,sid): Super (student,self).__init__(name,age) Self.grade=Grade Self.sid=Sid Self.enroll ()defTell (self):" "Self Introduction Method" "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 ( )
classRole (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= MoneydefShot (self):Print("Shooting ...")     defGot_shot (self):Print("Ah...,i got shot ...")     defBuy_gun (self,gun_name):Print("just bought%s"%gun_name) R1= Role ('Alex','Police','AK47 ') #生成一个角色r2 = Role ('Jack','Terrorist','B22 ') #生成一个角色

Python Learning Day06

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.