python class 2
//test.py
1 class Employee:
2 'all employee'
3 empCount = 0
4 def __init__(self, name, salary):
5 self.name = name
6 self.salary = salary
7 Employee.empCount += 1
8 def prt(self):
9 print 'self ', self
10 print '__class__', self.__class__
11 def displayCount(self):
12 print 'Total Employee %d' % Employee.empCount
13 def displayEmployee(self):
14 print 'Name: ', self.name, ', Salary: ', self.salary
15
16 ee = Employee('zcl', 0)
17 print 'doc', Employee.__doc__
18 ee.displayCount()
19 ee.displayEmployee()
20 ee.prt()
21 print 'hasattr', hasattr(ee, 'empCount'), ee.empCount
22 print 'getattr', getattr(ee, 'empCount')
23 print 'setattr', setattr(ee, 'empCount', 2), ee.empCount
24 print 'delattr', delattr(ee, 'empCount')
25 print 'hasattr', hasattr(ee, 'empCount')
26 print '__dict__', ee.__dict__
27 #print '__name__', ee.__name__
28 print '__module__', ee.__module__
29 #print '__bases__', ee.__bases__
30
31 class Name:
32 def __init__(self):
33 self.name = 'I am father'
34 def method(self):
35 print 'p', self.name
36 def vmethod(self):
37 print 'vp', 'I am parent method'
38
39 class Point(Name):
40 def __init__(self, x=0,y=0):
41 self.x = x
42 self.y = y
43 Name.__init__(self)
44 def __del__(self):
45 class_name = self.__class__.__name__
46 print class_name, 'destroied'
47 def submethod(self):
48 Name.method(self)
49 def vmethod(self):
50 print 'vp', 'I am child method'
51 def __repr__(self):
52 print '__repr__'
53 return 'repr finished'
54 def __cmp__(self, x):
55 if 1==x :
56 return 1
57 else:
58 return 2
59 def __add__(self, other):
60 return Point(self.x+other.x, self.y+other.y)
61 __privateData = "You can not see me, but properly can!"
62
63 pt1 = Point()
64 pt2 = pt1
65 pt3 = pt1
66
67 print id(pt1), id(pt2), id(pt3)
68 pt1.submethod()
69 pt1.vmethod()
70 print repr(pt1)
71 print cmp(pt1, 1)
72 del pt1
73 del pt2
74 del pt3
75 p3 = Point(1, 2)+Point(4, 5)
76 print 'p3.x, p3.y', p3.x, p3.y
77 print 'privateData ', p3._Point__privateData
//result
# python test.py
doc all employee
Total Employee 1
Name: zcl , Salary: 0
self <__main__.Employee instance at 0x7fa0a01c4200>
__class__ __main__.Employee
hasattr True 1
getattr 1
setattr None 2
delattr None
hasattr True
__dict__ {'salary': 0, 'name': 'zcl'}
__module__ __main__
140327857701664 140327857701664 140327857701664
p I am father
vp I am child method
__repr__
repr finished
1
Point destroied
Point destroied
Point destroied
p3.x, p3.y 5 7
privateData You can not see me, but properly can!
Point destroied
Finally:
python 支持多继承,这样一来,它就是c++的脚本版本了吧!!哈哈,但其它高级语言都在弱化多继承,在此,我也不多做讨论了
另外,多说一句,python里保护型成员用单下划线'_'
OK,就这么吧
python class 2的更多相关文章
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
- python开发编译器
引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...
- 关于解决python线上问题的几种有效技术
工作后好久没上博客园了,虽然不是很忙,但也没学生时代闲了.今天上博客园,发现好多的文章都是年终总结,想想是不是自己也应该总结下,不过现在还没想好,等想好了再写吧.今天写写自己在工作后用到的技术干货,争 ...
随机推荐
- DB2 rollforward 命令使用详解
DB2 rollforward 命令使用详解 原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1003wuc ...
- IIS8.5 Error Code 0x8007007e HTTP 错误 500.19的解决方法
window server 2012R2 IIS8.5 引用:https://www.52jbj.com/yunying/340443.html HTTP 错误 500.19 - Internal S ...
- {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll
Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...
- 关于FlexSlider插件
Flexslider选项设置 $(window).load(function() { $('.flexslider').flexslider({ animation: "fade" ...
- Oracle的一些经典SQL面试题
实例1:测试数据: create table nba( team varchar2(20), year number(4) ) SQL> select * from nba; TEAM ...
- Python:多线程
据廖雪峰老师的学习文档介绍,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程. Python的标准库提供了两个模块: ...
- MySQL命令:约束
约束是一种限制,它通过对表的行或列的数据做出限制,来确保表的数据的完整性.唯一性 约束分类: 约束类型与关键字: 主键 PRIMARY KEY 默认值 DEFAULT 唯一 UN ...
- python面向对象高级:枚举
在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数.这两种类型经常(但不总是)重叠. 枚举是一个被命名的整型常数的集合,枚举在日常生活中很常见,例 ...
- iOS 添加第三方.framework 打包上传iTunesConnect 遇到的坑
1.添加完第三方库,模拟器运行没事,打iOS通用设备包的时候报一个错. ld: '/Users/jiangwei.wang/Documents/Project/APP NAME/SeosMobileK ...
- 数学用语中的 透明 transitive property
1.透明 https://en.wikipedia.org/wiki/Equivalence_relation In mathematics, an equivalence relation is a ...