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实现获取序列中最小的几个元素
Sep 25 Python
shell命令行,一键创建 python 模板文件脚本方法
Mar 20 Python
python 按照固定长度分割字符串的方法小结
Apr 30 Python
对Xpath 获取子标签下所有文本的方法详解
Jan 02 Python
Python数据可视化库seaborn的使用总结
Jan 15 Python
Python之时间和日期使用小结
Feb 14 Python
Python变量类型知识点总结
Feb 18 Python
python set内置函数的具体使用
Jul 02 Python
numpy按列连接两个维数不同的数组方式
Dec 06 Python
Python类class参数self原理解析
Nov 19 Python
python基于socket模拟实现ssh远程执行命令
Dec 05 Python
pandas中pd.groupby()的用法详解
Jun 16 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
ninety plus是什么?ninety plus咖啡好吗?
2021/03/04 新手入门
关于PHP的相似度计算函数:levenshtein的使用介绍
2013/04/15 PHP
php switch语句多个值匹配同一代码块的实现
2014/03/03 PHP
2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题
2014/04/08 PHP
在PHP中运行Linux命令并启动SSH服务的例子
2014/06/12 PHP
thinkPHP基于ajax实现的菜单与分页示例
2016/07/12 PHP
PHP实现文件上传后台处理脚本
2020/03/04 PHP
javascript 中对象的继承〔转贴〕
2007/01/22 Javascript
详解JS 比较两个Json对象的值是否相等的实例
2013/11/20 Javascript
js智能获取浏览器版本UA信息的方法
2016/08/08 Javascript
Bootstrap模态框调用功能实现方法
2016/09/19 Javascript
jquery利用json实现页面之间传值的实例解析
2016/12/12 Javascript
JavaScript基本类型值-Number类型
2017/02/24 Javascript
Vue computed计算属性的使用方法
2017/07/14 Javascript
Vue如何从1.0迁移到2.0
2017/10/19 Javascript
Node配合WebSocket做多文件下载以及进度回传
2019/11/07 Javascript
关于IDEA中的.VUE文件报错 Export declarations are not supported by current JavaScript version
2020/10/17 Javascript
原生JavaScript实现购物车
2021/01/10 Javascript
Python 3.6 性能测试框架Locust安装及使用方法(详解)
2017/10/11 Python
python使用正则筛选信用卡
2019/01/27 Python
python中通过selenium简单操作及元素定位知识点总结
2019/09/10 Python
利用python、tensorflow、opencv、pyqt5实现人脸实时签到系统
2019/09/25 Python
python matplotlib中的subplot函数使用详解
2020/01/19 Python
python mock测试的示例
2020/10/19 Python
python将YUV420P文件转PNG图片格式的两种方法
2021/01/22 Python
Shopee越南:东南亚与台湾电商平台
2019/02/03 全球购物
女子锻炼服装和瑜伽服装:Splits59
2019/03/04 全球购物
新三好学生主要事迹
2014/01/23 职场文书
小区消防演习方案
2014/02/21 职场文书
高中学生期末评语
2014/04/25 职场文书
初中升旗仪式演讲稿
2014/05/08 职场文书
授权收款委托书范本
2014/10/10 职场文书
开展批评与自我批评发言稿
2014/10/16 职场文书
民主评议党员个人总结
2015/02/13 职场文书
文明礼貌主题班会
2015/08/14 职场文书
2016大学生国家助学贷款承诺书
2016/03/25 职场文书