python面向对象编写案例(Python面向对象程序设计示例小结)
类别:脚本大全 浏览量:961
时间:2022-04-04 14:42:28 python面向对象编写案例
Python面向对象程序设计示例小结本文实例讲述了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程序设计有所帮助。
原文链接:https://blog.csdn.net/fengshenyue/article/details/54692623
您可能感兴趣
- python自动计算机器人(python实现nao机器人手臂动作控制)
- python面向对象的介绍(Python面向对象思想与应用入门教程类与对象)
- python基于django的博客系统(利用django+wechat-python-sdk 创建微信服务器接入的方法)
- opencv抠出边缘检测的图形(python opencv实现图像边缘检测)
- pythondict排序原理(Python标准库使用OrderedDict类的实例讲解)
- python yield 使用浅析(yii框架使用分页的方法分析)
- 如何使用github中的python库(使用GitHub和Python实现持续部署的方法)
- python3.9.1如何安装模块(Python3.6中Twisted模块安装的问题与解决)
- 2021-10-07 00:38:09
- python flask部署实例(Python Flask框架扩展操作示例)
- python元组操作方法(Python元组常见操作示例)
- python自带的ide的使用(基于python的ini配置文件操作工具类)
- python list基本操作(Python3中列表list合并的四种方法)
- 零基础学计算机二级python(计算机二级python学习教程1 教大家如何学习python)
- python的decode函数在哪个模块(python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决)
- python搭建django框架(详解Python网络框架Django和Scrapy安装指南)
- 鲢鳙钓底还是钓浮 流水的水域应怎样做钓(鲢鳙钓底还是钓浮)
- 入秋后的第二场苹果发布会来了 全新M1系列芯片登场(入秋后的第二场苹果发布会来了)
- 苹果正式发布自研芯片M1 5nm 32核心 彻底放弃Intel(苹果正式发布自研芯片M1)
- 苹果自研芯片跑分对比 A16芯片排名靠后,M1系列霸榜(苹果自研芯片跑分对比)
- X86处理器的梦魇 苹果M1自研芯片到底有多强(苹果M1自研芯片到底有多强)
- 泰剧《爱欲之神》Boom kitkong和Great合体杂志(泰剧爱欲之神Boomkitkong和Great合体杂志)
热门推荐
- css中三角标(使用css实现三角符号效果)
- 面试中如何给人留下好印象
- python面向对象基本思想(详解Python:面向对象编程)
- .NET中生成带Logo的二维码
- cpu怎么访问php(服务器大量php-cgi.exe进程导致CPU占用100%的解决方法)
- python基本运算符编程(对Python3 * 和 ** 运算符详解)
- mysql服务器端安装步骤(windows下jsp+mysql网站环境配置方法)
- mysql表结构设计分享(MySql开发之自动同步表结构)
- canvas如何保存当前的图片(canvas如何实现多张图片编辑的图片编辑器)
- dedecms当天文章总数(dedecms5.7文章二次开发实现阅读全文功能的方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9