终于学到PYTHON的类啦~~
2013-01-29 11:42
211 查看
#!/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()
# 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第三个小程序,终于学到爬虫了~~~,利用有道,爬出个在线翻译小程序~~~~~
- 终于忍不住,装上IDE开始Python之旅了
- 笨方法学Python 习题 22: 到现在你学到了哪些东西?
- 今天终于完成了美的网站的维护工作,工作中感觉又学到了点东西
- Facebook 发布开源框架 PyTorch, Torch 终于被移植到 Python 生态圈
- 最近学到的技巧(python调试信息的获取等)
- 神级程序员历经4天废寝忘食终于用python解决游戏机器人逃跑心得
- boost 1.34 终于简化了内嵌python的支持
- 今天终于学到了快速转载CSDN上的博客~~
- 不做版本帝,16 年后这个 Python 库终于发布 1.0 版
- 终于成功地在AIX 5.1/5.2/5.3上编译了Python 2.3.5/2.4.1/2.4.2
- 用Python物理建模的第一本书终于来啦
- 终于解决了python 3.x import cv2 “ImportError: DLL load failed: 找不到指定的模块” 的问题
- 【实现】[抓取知网论文标题摘要] 终于能用Sublime直接编译运行python了,可是却无法导入bs4 | BeautifulSoup
- Python爬虫学到什么样就可以找工作了?
- [python]numpy终于到的函数
- Python使用PyQt5库 一、Designer界面的使用——终于出界面了。****************
- 终于在pycharm下(Python3.6.1版本)安装完成机器学习相关库文件(sklearn scikit-learn gensim xgboost tensorflow nltk )
- Apache+python 在cookie存读上卡了好久,终于搞明白怎么回事了。
- python-nlp-学到的一点东西