#!/usr/bin/python
# Filename:objvar.py

class Robot:
    '''Represents a robot,with a name.'''

    population = 0

    def __init__(self,name):
        '''Iinitializes the data.'''
        self.name = name
        print('(Initialize {0})'.format(self.name))

        Robot.population += 1

    def __del__(self):
        '''I am dying'''
        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):
        '''Greeting by the robot.

     
        Yeah,they can do that.'''
        print('Greetings, my master call me{0}.'.format(self.name))

    def howMany():
        '''Print the current population.'''
        print('We have {0:d} robots.'.format(Robot.population))
    howMany = staticmethod(howMany)

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("Robots have finished their work. So let's destroy them.")

del droid1
del droid2

Robot.howMany()

终于学到PYTHON的类啦~~

相关文章:

  • 2022-12-23
  • 2021-09-04
  • 2022-02-07
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-20
猜你喜欢
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2022-12-23
  • 2021-09-08
  • 2021-07-05
相关资源
相似解决方案