Python面向对象程序设计示例小结


Posted in Python onJanuary 30, 2019

本文实例讲述了Python面向对象程序设计。分享给大家供大家参考,具体如下:

示例1:

#encoding:utf-8
'''example 1
class test:
  def __init__(self,year,**arg):
    self.year = year
    self.args = arg
  def kaka(self):
    if isinstance(self.year,str):
      print 'input\'s year is a string! Error'
    elif isinstance(self.year,int):
      a = self.year%4
      print a
    else:
      print 'Error!'
  def deal_arg(self):
    # for v in self.args:
    #  print '\n====================\n',v
    for k in self.args:
      print str(k)+'\tvalue is '+str(self.args[k])
    print self.args
a = test(2014,a=123,b=321)
a.kaka()
a.deal_arg()

运行结果:

2
a value is 123
b value is 321
{'a': 123, 'b': 321}

示例2:

#encoding:utf-8
'''example 2'''
class test:
  '这是一个测试的基类'
  def __init__(self,test):
    self.test = test
  '这是一个测试的基类'
print 'test.__doc__:',test.__doc__
print 'test.__name__:',test.__name__
print 'test.__module__:',test.__main__
print 'test.__bases__:',test.__bases__
print 'test.__dict__:',test.__dict__

示例3:

'''example 3 Class inheritance and method partial rewriting'''
class parent:
  def __init__(self):
    print '这是一个父类'
  def ParentsMethond(self):
    print '这是一个父类方法'
  def Parenttest(self,arg):
    self.arg = 'This is a test!'
    print '父类的self变量: %s' %self.arg
    parent.arg = arg
    print '父类的变量: %s' %parent.arg
class child(parent):
  """docstring for child"""
  def __init__(self):
    print '这是一个子类'
  def ChildMethod(self):
    print '调用子类方法 child method'
  def ParentsMethond(self):
    print '父类方法重写!!!!!!!!!!!!!!!!!!!!'
b= parent()
c = child()
c.ChildMethod()
print '*'*10
b.ParentsMethond()
c.ParentsMethond()
print '*'*10
c.Parenttest(3899)

运行结果:

这是一个父类
这是一个子类
调用子类方法 child method
**********
这是一个父类方法
父类方法重写!!!!!!!!!!!!!!!!!!!!
**********
父类的self变量: This is  a test!
父类的变量: 3899

示例4:

'''example 4 Operator overloading'''
class test:
  def __init__(self,a,b):
    self.a = a
    self.b = b
  def __str__(self):
    return 'Vector (%d,%d)' % (self.a,self.b)
  def __add__(self,other):
    return test(self.a+other.a,self.b+other.b)
v1 = test(21,22)
v2 = test(2,3)
print v1 + v2

运行结果:

Vector (23,25)

示例5:

'''#example 5 private class'''
class JustCounter(object):
  """docstring for JustCounter"""
  __secretCount = 0 #私有变量
  publicCount = 0 #公开变量
  def count(self):
    self.__secretCount +=1
    self.publicCount +=1
    print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()
counter.count()
counter.count()
counter.count()
print counter.publicCount
print counter.__secretCount #报错,实例不能访问私有变量
print counter._JustCounter__secreCount

感兴趣的朋友可以测试上述代码运行效果。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python的re模块正则表达式操作
May 25 Python
python中模块的__all__属性详解
Oct 26 Python
Python实现图片滑动式验证识别方法
Nov 09 Python
在windows下Python打印彩色字体的方法
May 15 Python
pyhanlp安装介绍和简单应用
Feb 22 Python
Python 中Django安装和使用教程详解
Jul 03 Python
Python中的类与类型示例详解
Jul 10 Python
pygame实现贪吃蛇游戏(上)
Oct 29 Python
如何用OpenCV -python3实现视频物体追踪
Dec 04 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
Apr 15 Python
Django通过json格式收集主机信息
May 29 Python
Python监听键盘和鼠标事件的示例代码
Nov 18 Python
python实现浪漫的烟花秀
Jan 30 #Python
新年快乐! python实现绚烂的烟花绽放效果
Jan 30 #Python
python+selenium 定位到元素,无法点击的解决方法
Jan 30 #Python
解决Python selenium get页面很慢时的问题
Jan 30 #Python
对python实现模板生成脚本的方法详解
Jan 30 #Python
ActiveMQ:使用Python访问ActiveMQ的方法
Jan 30 #Python
python 发送和接收ActiveMQ消息的实例
Jan 30 #Python
You might like
adodb与adodb_lite之比较
2006/12/31 PHP
PHP框架实现WebSocket在线聊天通讯系统
2019/11/21 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
2019/12/12 PHP
js压缩利器
2007/02/20 Javascript
读jQuery之四(优雅的迭代)
2011/06/20 Javascript
JS跨域总结
2012/08/30 Javascript
js 获取页面高度和宽度兼容 ie firefox chrome等
2014/05/14 Javascript
防止Node.js中错误导致进程阻塞的办法
2016/08/11 Javascript
基于dataset的使用和图片延时加载的实现方法
2017/12/11 Javascript
微信小程序实现动态设置placeholder提示文字及按钮选中/取消状态的方法
2017/12/14 Javascript
vue项目中jsonp跨域获取qq音乐首页推荐问题
2018/05/30 Javascript
JS对象与json字符串相互转换实现方法示例
2018/06/14 Javascript
Vue使用预渲染代替SSR的方法
2020/07/02 Javascript
基于VSCode调试网页JavaScript代码过程详解
2020/07/20 Javascript
vue cli 3.0通用打包配置代码,不分一二级目录
2020/09/02 Javascript
连接Python程序与MySQL的教程
2015/04/29 Python
python实现简单socket通信的方法
2016/04/19 Python
快速入门python学习笔记
2017/12/06 Python
Python初学者需要注意的事项小结(python2与python3)
2018/09/26 Python
python将类似json的数据存储到MySQL中的实例
2019/07/12 Python
基于TensorFlow常量、序列以及随机值生成实例
2020/01/04 Python
django中cookiecutter的使用教程
2020/12/03 Python
美国正版电视节目和电影在线观看:Hulu
2018/05/24 全球购物
高三自我鉴定怎么写
2013/10/19 职场文书
缅怀革命先烈演讲稿
2014/05/14 职场文书
圆明园纪录片观后感
2015/06/03 职场文书
2016年中秋节慰问信
2015/12/01 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书
成功的商业计划书这样写才最靠谱
2019/07/12 职场文书
详解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
2021/04/25 Python
Redis 哨兵集群的实现
2021/06/18 Redis
你需要掌握的20个Python常用技巧
2022/02/28 Python
MyBatis核心源码深度剖析SQL语句执行过程
2022/05/20 Java/Android
SQL Server使用PIVOT与unPIVOT实现行列转换
2022/05/25 SQL Server
利用Python实时获取steam特惠游戏数据
2022/06/25 Python
JavaScript实现简单的音乐播放器
2022/08/14 Javascript