PYTHON--CLASS

复制代码
class Robot:
    population = 0
    def __init__(self, name):
        self.name = name
        print("(Initializing {0})".format(self.name))
        Robot.population += 1

    def die(self):
        print("{0} is being destroyed!".format(self.name))
        Robot.population -= 1

        if  Robot.population == 0:
            print("{0} was the last one.".format(self.name))
        else:
            print("There are still {0:d} robots working.".format(Robot.population))
    def  sayHi(self):
        print("Greetings, my masters call me {0}.".format(self.name))
    
    @classmethod
    def howMany(cls):
        print("We have {0:d} robots.".format(cls.population))

droid1 = Robot("R2-D2")
droid1.sayHi()
Robot.howMany()

droid2 = Robot("C-3P0")
droid2.sayHi()
Robot.howMany()

print("\nRobots can do some work here.\n")
print("Robot have finished their work. So let's destroy them.\n")

droid1.die()
droid2.die()

Robot.howMany()

print("=================")

class SchoolMember:
    def __init__(self, name, age):
        self.name =name
        self.age =age
        print('(Initialized SchoolMember: {0})'.format(self.name))

    def tell(self):
        print('Name:"{0}" Age:"{1}"'.format(self.name, self.age), end = " ")

class Teacher(SchoolMember):
    """docstring for ClassName"""
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print('(Initialized Teacher: {0})'.format(self.name))

    def  tell(self):
        SchoolMember.tell(self)
        print('Salary:"{0:d}"'.format(self.salary))

class Student(SchoolMember):
    """docstring for ClassName"""
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print('(Initialized Student: {0})'.format(self.name))
        
    def  tell(self):
        SchoolMember.tell(self)
        print('Marks:"{0:d}"'.format(self.marks))


t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)

print()

members = [t, s]
for member in members:
    member.tell()

        
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值