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实现的简单万年历例子分享
Apr 25 Python
Python实现端口复用实例代码
Jul 03 Python
Python入门篇之字符串
Oct 17 Python
Python fileinput模块使用实例
Jun 03 Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
Apr 17 Python
Python中扩展包的安装方法详解
Jun 14 Python
Python中常用信号signal类型实例
Jan 25 Python
python调用API实现智能回复机器人
Apr 10 Python
python爬虫之xpath的基本使用详解
Apr 18 Python
python selenium 执行完毕关闭chromedriver进程示例
Nov 15 Python
解决TensorFlow模型恢复报错的问题
Feb 06 Python
Python实现初始化不同的变量类型为空值
Jun 02 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
海贼王:最美的悬赏令!
2020/03/02 日漫
PHP hex2bin()函数用法讲解
2019/02/25 PHP
js getBoundingClientRect() 来获取页面元素的位置
2010/11/25 Javascript
javascript中setInterval的用法
2015/07/19 Javascript
20分钟成功编写bootstrap响应式页面 就这么简单
2016/05/12 Javascript
JS判断图片是否加载完成方法汇总(最新版)
2016/05/13 Javascript
AngularJS中transclude用法详解
2016/11/03 Javascript
Vue.js组件通信的几种姿势
2017/10/23 Javascript
JavaScript实现求最大公共子串的方法
2018/02/03 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
用原生JS实现爱奇艺首页导航栏代码实例
2019/09/19 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
Vue 3.0双向绑定原理的实现方法
2019/10/23 Javascript
[01:20]DOTA2更新全新英雄 天涯墨客现已加入游戏
2018/08/25 DOTA
[01:52]PWL S2开团时刻第四期——DOTA2成语故事
2020/12/03 DOTA
Python的Tornado框架的异步任务与AsyncHTTPClient
2016/06/27 Python
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
2017/08/08 Python
实用自动化运维Python脚本分享
2018/06/04 Python
Python 使用PyQt5 完成选择文件或目录的对话框方法
2019/06/27 Python
python 并发编程 阻塞IO模型原理解析
2019/08/20 Python
Python API自动化框架总结
2019/11/12 Python
Python小程序之在图片上加入数字的代码
2019/11/26 Python
python 操作hive pyhs2方式
2019/12/21 Python
一篇文章教你用python画动态爱心表白
2020/11/22 Python
Html5之webcoekt播放JPEG图片流
2020/09/22 HTML / CSS
如果一个类实现了多个接口但是这些接口有相同的方法名将会怎样
2013/06/16 面试题
家长会学生家长演讲稿
2013/12/29 职场文书
最新创业融资计划书
2014/01/19 职场文书
《唯一的听众》教学反思
2014/02/20 职场文书
技能竞赛活动方案
2014/02/21 职场文书
行政人事专员岗位职责
2014/03/05 职场文书
销售员态度差检讨书
2014/10/26 职场文书
神秘岛读书笔记
2015/07/01 职场文书
2016年国庆节新闻稿范文
2015/11/25 职场文书
win10+anaconda安装yolov5的方法及问题解决方案
2021/04/29 Python
mysql 排序失效
2022/05/20 MySQL