python面向对象例子
2016-07-27 15:47
375 查看
#!/usr/bin/python # -*- coding: UTF-8 -*- __author__ = 'Administrator' #面向对象 class Person: count=0; def __init__(self,name,age): self.name=name; self.age=age; Student.count+=1; def __del__(self): print("调用析构函数"); def displayCount(self): print("总人数:%d " %Student.count); def __repr__(self): print("调用__repr__"); def __str__(self): print("调用__str__"); return "name="+self.name+",age="+str(self.age); def __cmp__(self, other): print("调用__cmp__"); return cmp(self.age,other.age); def displayStudent(self): print("name="+self.name+",age="+str(self.age)); def test(self): print("test"); pass class Student(Person): money=100; __city="beijing"# 私有变量 def displayStudent(self): print("name="+self.name+",age="+str(self.age)+",money="+str(self.money)+",__city="+self.__city); if __name__=="__main__": stu1=Student("xiaoming",22); stu1.displayCount(); stu1.displayStudent(); stu2=Student("xiaohua","11"); stu2.displayCount(); stu2.displayStudent(); person1 =Person("xiaohong","11"); person1.displayCount(); person1.displayStudent(); print(stu1); print(stu2); print(person1); print(person1==stu2) person1.test(); #print(stu2.__city);
相关文章推荐
- python 实战 登录注册
- python--gevent协程及协程概念
- python 切片
- Python 接口测试
- python中的闭包
- 安装virtualenv No module named virtualenv
- python virtualenv环境运行django
- python3 匹配任意字符
- python sqlite3 的事务控制
- Python选择网卡发包及接收数据包
- python sklearn-01:机器学习基础
- python学习笔记2(对象)
- python入门笔记
- Python 第三方模块安装出现的问题和解决方案.
- python--multiprocessing多进程总结
- 203. Remove Linked List Elements
- Boost python 编程记录
- python的正则表达式
- Python GIL
- 【Python学习】Thread笔记(1)