zoukankan      html  css  js  c++  java
  • Python的OO思想

    想当年大二的时候,在学校学习Java,

    最牛逼的OO思想,用了3页纸就讲完了,还是清华大学出版社的呢。

    后来全凭自己啃视频,啃代码才搞懂什么叫做OO。

    现在学习Python,就用自己的方式,好好学习一次:

    OO的基本,是封装,继承,多态。

    首先是继承:

    定义一个类:

    Python代码  收藏代码
    1. class Bird(object):  
    2.     have_feather = True  
    3.     way_of_reproduction  = 'egg'  

    调用这个类:

    Python代码  收藏代码
    1. summer = Bird()  
    2. print summer.way_of_reproduction  

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

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

    Python代码  收藏代码
    1. class Bird(object):      
    2.     have_feather = True      
    3.     way_of_reproduction = 'egg'      
    4.       
    5.     def say(self, word='hi hi'):  
    6.               print 'i say :' + word  

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

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

    Python代码  收藏代码
    1. chk = Chicken()  
    2. print chk.have_feather  
    3. print chk.sat('hello')  

    __init__()方法

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

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

    如:

    Python代码  收藏代码
    1. class happyBird(Bird):  
    2.     def __init__(self,more_words):  
    3.         print 'We are happy birds.',more_words  
    4.   
    5. hb = happyBird('Happy,Happy!')  

    父类方法的重载:

    Python代码  收藏代码
    1. class Hello(object):  
    2.     name = 'hello'  
    3.       
    4.     def __init__(self):  
    5.         self.name='my name is hello'  
    6.       
    7.     #类中的参数必须带有self参数  
    8.     def sayhi(self):  
    9.         print 'hi you'  
    10.   
    11. class World(Hello):   
    12.     def __init__(self):  
    13.         #这里访问的是父类初始化的变量名  
    14.         print 'before:',Hello.name   
    15.         super(World,self).__init__()    
    16.         #由于调用了父类的初始化构造函数,继承了父类的变量的改变  
    17.         print 'after:',self.name  
    18.           
    19.         #近似于方法重载  
    20.     def sayhi(self,word='baby'):  
    21.         #调用父类sayhi方法  
    22.         super(World,self).sayhi()  
    23.         print 'hi '+word  
    24.               
    25.     def sayWorld(self):  
    26.         print 'hi,hello world'  
    27.           
    28. if __name__ == '__main__':  
    29.     c = World()  
    30.     c.sayhi()  
    31.     c.sayWorld()  

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

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

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

    Python代码  收藏代码
    1. isinstance(object, classinfo)   
    2. 判断实例是否是这个类或者object是变量  
    3.   
    4. classinfo 是类型(tuple,dict,int,float)  
    5. 判断变量是否是这个类型   
    6.   
    7. class objA:   
    8. pass   
    9.   
    10. A = objA()   
    11. B = 'a','v'   
    12. C = 'a string'   
    13.   
    14. print isinstance(A, objA)   
    15. print isinstance(B, tuple)   
    16. print isinstance(C, basestring)   
    17. 输出结果:   
    18. True   
    19. True   
    20. True   

      

  • 相关阅读:
    控制C++的类只能在堆分配或只能在栈分配
    static 相关随笔
    虚拟继承
    虚函数和纯虚函数有以下所示方面的区别(转)
    构造函数 析构函数
    标准C++中有没有接口和纯抽象类的概念?(转)
    抽象类和接口(转)
    如何快速正确的安装 Ruby, Rails 运行环境
    NSRunLoop
    UIImagePickerController之死因 .
  • 原文地址:https://www.cnblogs.com/rrxc/p/4157789.html
Copyright © 2011-2022 走看看