Python的面向对象思想分析


Posted in Python onJanuary 14, 2015

本文实例讲述了Python的面向对象思想。分享给大家供大家参考。具体分析如下:

面向对象的基本思想是封装,继承,多态。

首先是继承:

定义一个类:

class Bird(object):  

    have_feather = True  

    way_of_reproduction  = 'egg'

调用这个类:

summer = Bird()  

print summer.way_of_reproduction

与Java不同是,Python是不需要new来实例化类的。

同样,Python的类下面是可以定方法的:

class Bird(object):      

    have_feather = True      

    way_of_reproduction = 'egg'      

      

    def say(self, word='hi hi'):  

              print 'i say :' + word

注意一点,所有类的函数,必须至少带有一个参数,这个参数必须是self。

类以外的函数没有这一个限制。

chk = Chicken()  

print chk.have_feather  

print chk.sat('hello')

__init__()方法

__init__()是一个特殊方法(special method)。Python里会有一些特殊方法,Python会以特别的方式处理它们。特殊方法的名字的特点是前后都有两个下划线。

__init__()方法的特殊在于,如果你在类中定义了这个方法,一旦你根据这个类建立对象,Python就会自动调用这个方法(这个过程也叫初始化)。

如:

class happyBird(Bird):  

    def __init__(self,more_words):  

        print 'We are happy birds.',more_words  

  

hb = happyBird('Happy,Happy!')

父类方法的重载:

class Hello(object):  

    name = 'hello'  

      

    def __init__(self):  

        self.name='my name is hello'  

      

    #类中的参数必须带有self参数  

    def sayhi(self):  

        print 'hi you'  

  

class World(Hello):   

    def __init__(self):  

        #这里访问的是父类初始化的变量名  

        print 'before:',Hello.name   

        super(World,self).__init__()    

        #由于调用了父类的初始化构造函数,继承了父类的变量的改变  

        print 'after:',self.name  

          

        #近似于方法重载  

    def sayhi(self,word='baby'):  

        #调用父类sayhi方法  

        super(World,self).sayhi()  

        print 'hi '+word  

              

    def sayWorld(self):  

        print 'hi,hello world'  

          

if __name__ == '__main__':  

    c = World()  

    c.sayhi()  

    c.sayWorld()

 另外,python是允许多继承的,但是这个是个非常危险的操作,建议不要随便使用。

关于Python的多态,就像JavaScript一样,直接访问对象的属性,不需要使用接口,没有类型转换。

对于类型的判断,有抓们的type()函数,和isinstance()函数判断是否某个函数的子类。

isinstance(object, classinfo)

判断实例是否是这个类或者object是变量 
 
classinfo 是类型(tuple,dict,int,float) 
判断变量是否是这个类型  
class objA:   

pass   

  

A = objA()   

B = 'a','v'   

C = 'a string'   

  

print isinstance(A, objA)   

print isinstance(B, tuple)   

print isinstance(C, basestring)

输出结果:  
True  
True  
True 

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

Python 相关文章推荐
python使用正则搜索字符串或文件中的浮点数代码实例
Jul 11 Python
Python文件操作类操作实例详解
Jul 11 Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
Nov 13 Python
Python 3中的yield from语法详解
Jan 18 Python
Python中多个数组行合并及列合并的方法总结
Apr 12 Python
Python 变量类型详解
Oct 10 Python
对python 判断数字是否小于0的方法详解
Jan 26 Python
详解python项目实战:模拟登陆CSDN
Apr 04 Python
Anaconda+Pycharm环境下的PyTorch配置方法
Mar 13 Python
Python接口测试环境搭建过程详解
Jun 29 Python
如何用PyPy让你的Python代码运行得更快
Dec 02 Python
一文搞懂如何实现Go 超时控制
Mar 30 Python
为python设置socket代理的方法
Jan 14 #Python
Python单例模式实例分析
Jan 14 #Python
python文件读写操作与linux shell变量命令交互执行的方法
Jan 14 #Python
Python中使用Tkinter模块创建GUI程序实例
Jan 14 #Python
更改Python命令行交互提示符的方法
Jan 14 #Python
Python的迭代器和生成器使用实例
Jan 14 #Python
python实现带验证码网站的自动登陆实现代码
Jan 12 #Python
You might like
PHP 字符截取 解决中文的截取问题,不用mb系列
2009/09/29 PHP
PHP将XML转数组过程详解
2013/11/13 PHP
ThinkPHP CURD方法之where方法详解
2014/06/18 PHP
windows下的WAMP环境搭建图文教程(推荐)
2017/07/27 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
2020/02/15 PHP
获取服务器传来的数据 用JS去空格的正则表达式
2012/03/26 Javascript
Jquery多选框互相内容交换的实例代码
2013/07/04 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
JQuery 图片滚动轮播示例代码
2014/03/24 Javascript
JavaScript实现当网页加载完成后执行指定函数的方法
2015/03/21 Javascript
js限制文本框的输入内容代码分享(3类)
2015/08/20 Javascript
JQuery datepicker 用法详解
2015/12/25 Javascript
浅谈angular2的http请求返回结果的subcribe注意事项
2017/03/01 Javascript
angular使用bootstrap方法手动启动的实例代码
2017/07/18 Javascript
vue.js组件vue-waterfall-easy实现瀑布流效果
2017/08/22 Javascript
Node.js log4js日志管理详解
2018/07/31 Javascript
angular6 填坑之sdk的方法
2018/12/27 Javascript
微信小程序配置服务器提示验证token失败的解决方法
2019/04/03 Javascript
Layui实现主窗口和Iframe层参数传递
2019/11/14 Javascript
Vue 实现v-for循环的时候更改 class的样式名称
2020/07/17 Javascript
Python中使用gzip模块压缩文件的简单教程
2015/04/08 Python
Windows下Anaconda的安装和简单使用方法
2018/01/04 Python
python 自动批量打开网页的示例
2019/02/21 Python
tensorflow 变长序列存储实例
2020/01/20 Python
pytorch实现CNN卷积神经网络
2020/02/19 Python
python中字符串的编码与解码详析
2020/12/03 Python
韩国江南富人区高端时尚百货商场:Galleria(格乐丽雅)
2018/03/27 全球购物
利达恒信公司.NET笔试题面试题
2016/03/05 面试题
电子技术专业中专生的自我评价
2013/12/17 职场文书
运动会入场口号
2014/06/07 职场文书
部队反四风对照检查材料
2014/09/26 职场文书
给女朋友道歉的话大全
2015/01/20 职场文书
2015财务年度工作总结范文
2015/05/04 职场文书
运动会三级跳加油稿
2015/07/21 职场文书
在python中实现导入一个需要传参的模块
2021/05/12 Python
Python使用Web框架Flask开发项目
2022/06/01 Python