python入门第三天——类
来源:互联网 发布:上古卷轴5清理脏数据 编辑:程序博客网 时间:2024/06/08 01:33
# # -*-coding:utf-8-*-class Person: personnumber = 0 def __init__(self,name): self.name=name print 'Initializing %s'%self.name Person.personnumber +=1 def __del__(self):##在程序结束时,自动销毁对象 print '%s say bye.'%self.name Person.personnumber -=1 if Person.personnumber==0: print 'I am the last one' print 'destory the %s' % self.name else: print 'There are still %d people left'%Person.personnumber print 'destory the %s' % self.name def sayHi(self): print 'Hi my name is %s' % self.name def howMany(self): if Person.personnumber==1: print 'i am the only person here' else: print 'there are %d person '%Person.personnumberkalam = Person("Kalam")kalam.sayHi()kalam.howMany()print '\n'swaroop = Person("Swaroop")swaroop.sayHi()swaroop.howMany()print 'over\n'对应运行结果:Initializing KalamHi my name is Kalami am the only person hereInitializing SwaroopHi my name is Swaroopthere are 2 person over##del执行的一步Kalam say bye.There are still 1 people leftdestory the KalamSwaroop say bye.I am the last onedestory the Swaroop###del执行,对象被销毁,在程序的所有代码运行完之后再销毁对象###init方法用一个名字来初始化person实例
阅读全文
0 0
- python入门第三天——类
- python入门第三天——继承
- python入门第三天——赋值区别
- python入门第三天——文件/存储器
- 三天入门Python---文件操作(第三天)
- python学习——第三天
- Python Learning——第三天
- 21天学通python——第三天
- 三天入门Python---基本语法(第一天)
- 8天入门wpf—— 第三天 样式
- 8天入门wpf—— 第三天 样式
- 8天入门wpf—— 第三天 样式
- 8天入门wpf—— 第三天 样式
- 【转载】8天入门wpf—— 第三天 样式
- 8天入门wpf—— 第三天 样式
- php入门第三天
- web入门第三天
- Lucene第三天入门
- vue中的路由及自定义图标
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- 图遍历的算法
- Go-gin的基本使用
- string总结
- python入门第三天——类
- 李航《统计学习方法》错误汇总修编
- 关于整数的二进制中1的个数
- 浙江省多媒体竞赛及宁波一行
- 树存储结构的几种表示方法
- 嵌入式实验(一)安装交叉编译工具链arm-none-linux-gnueabi-时遇到的问题
- Go-实现程序零点定时启动
- 在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件
- BZOJ3837: [Pa2013]Filary