python学习day06

Posted 小_龟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=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的主要内容,如果未能解决你的问题,请参考以下文章

python学习day06

python 学习DAY06

python 学习笔记day06-错误和异常函数基础

Python学习day1作业

Python学习之旅—Day06(字符串+字典+列表重点方法)

Python自动化基础day06:面向对象学习