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获取beautifulphoto随机某图片代码实例
Dec 18 Python
python删除文件示例分享
Jan 28 Python
python字典序问题实例
Sep 26 Python
TensorFlow实现非线性支持向量机的实现方法
Apr 28 Python
python对于requests的封装方法详解
Jan 03 Python
python实现矩阵打印
Mar 02 Python
anaconda如何查看并管理python环境
Jul 05 Python
详解Django模版中加载静态文件配置方法
Jul 21 Python
Django Celery异步任务队列的实现
Jul 24 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
Sep 10 Python
python的pyecharts绘制各种图表详细(附代码)
Nov 11 Python
python实现微信打飞机游戏
Mar 24 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
剧场版动画《PSYCHO-PASS 3 FIRST INSPECTOR》3月27日日本上映!
2020/03/06 日漫
用PHP代码在网页上生成图片
2015/07/01 PHP
php使用number_format函数截取小数的方法分析
2016/05/27 PHP
javascript 窗口加载蒙板 内嵌网页内容
2010/11/19 Javascript
初学js 新节点的创建 删除 的步骤
2011/07/04 Javascript
使用indexOf等在JavaScript的数组中进行元素查找和替换
2013/09/18 Javascript
Node.js中使用mongoskin操作mongoDB实例
2014/09/28 Javascript
jQuery实现折叠、展开的菜单组效果代码
2015/09/16 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
轮播的简单实现方法
2016/07/28 Javascript
jQuery+Pdo编写login登陆界面
2016/08/01 Javascript
微信小程序图片宽100%显示并且不变形
2017/06/21 Javascript
AngularJS 事件发布机制
2018/08/28 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
bootstrap 日期控件 datepicker被弹出框dialog覆盖的解决办法
2019/07/09 Javascript
[34:39]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#1COL VS EG第二局
2016/03/05 DOTA
[00:12]DAC SOLO赛卫冕冠军 VG.Paparazi灬展现SOLO技巧
2018/04/06 DOTA
python使用urllib模块和pyquery实现阿里巴巴排名查询
2014/01/16 Python
介绍Python的Django框架中的静态资源管理器django-pipeline
2015/04/25 Python
python常见数制转换实例分析
2015/05/09 Python
Python 类与元类的深度挖掘 I【经验】
2016/05/06 Python
完美解决Python2操作中文名文件乱码的问题
2017/01/04 Python
python获取当前运行函数名称的方法实例代码
2017/04/06 Python
Python3实现简单可学习的手写体识别(实例讲解)
2017/10/21 Python
Python日期时间Time模块实例详解
2019/04/15 Python
Python如何优雅获取本机IP方法
2019/11/10 Python
Keras搭建自编码器操作
2020/07/03 Python
REISS英国官网:伦敦High Street最受欢迎品牌
2016/12/21 全球购物
将世界上最美丽的摄影作品转化为艺术作品:Photos.com
2017/11/28 全球购物
都柏林通行卡/城市通票:The Dublin Pass
2020/02/16 全球购物
Hotels.com韩国:海外国内旅行所需的酒店和住宿预订网站
2020/05/08 全球购物
个人自我鉴定怎么写
2013/10/28 职场文书
妇联主席先进事迹
2014/05/18 职场文书
投标人法定代表人授权委托书格式
2014/09/28 职场文书
小学少先队工作总结2015
2015/05/26 职场文书
Java9新特性对HTTP2协议支持与非阻塞HTTP API
2022/03/16 Java/Android