python 类定义 继承

0 前言

  系统:win7 64bit 

  IDE : python(x,y) 2.7.6.1

  IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32

1 单继承

1.1 定义一个时间类,带默认参数

class mytime(object):
    def __init__(self,hour=21, \
                 minute=13, \
                 second=50):
        self.hour   = hour
        self.minute = minute
        self.second = second
        
    def __str__(self):
        return '__str__ hour:minute:second = %02d:%02d:%02d' \
               %(self.hour,self.minute,self.second)
    
    def get_hour(self):
        return self.hour
    
    def get_minute(self):
        return self.minute
    
    def get_second(self):
        return self.second

    def print_time(self):
        print  'hour:minute:second = %02d:%02d:%02d' %(self.hour,self.minute,self.second)
  

说明:  构造函数     __init__(self,hour=21, minute=13, second=50)  带默认参数的定义方式 ;

     用于输出的函数    __str__

           得到某属性值的方法    get_xxxx

           输出属性值的方法 print_time

1.2 定义日期时间类,继承时间类的属性和方法

class mydatetime(mytime):
    def __init__(self,year = 2016,mounth = 1,day = 1, \
                      hour=21, minute=13,second=50):
        super(mydatetime,self).__init__(hour,minute,second)
        self.year = year
        self.mounth = mounth
        self.day = day
        
    def __str__(self):
        return '__str__ year--mounth--day = %02d--%02d--%02d' \
               %(self.year,self.mounth,self.day)
    
    def __del__(self):
        """ __del__ mydatetime destroyed """
        print "__del__ mydatetime destroyed"
    
    def print_date(self):
        print  'year-mounth-day = %04d-%02d-%02d' \
        %(self.year,self.mounth,self.day)
#        self.print_time()  

 说明:   mydatetime 继承 mytime 的属性值,这里mydatetime(子类),mytime(父类)

    构造函数     __init__(self,year = 2016,mounth = 1,day = 1, hour=21, minute=13,second=50)  带默认参数的定义方式 ,注意要通过 super初始化父类属性值;

    用于输出的函数    __str__

        输出属性值的方法 print_date

1.3 测试

if __name__ == '__main__':

    print "\n"
    print "--"*10," mytime t0:"   
    t0 = mytime()
    print t0
    t0.print_time()
    print t0.get_hour()
    
    print "\n"
    print "--"*10," mytime t1:"     
    t1 = mytime(hour=7,second=20)
    t1.print_time()
    
    print "\n"
    print "--"*10," mydatetime da:"   
    da = mydatetime(minute=40)
    print da
#    da.print_date()
    da.print_time()
    del da

  输出:

>>> runfile('F:/Python/hysrc_py/untitled0_class.py', wdir=r'F:/Python/hysrc_py')


--------------------  mytime t0:
__str__ hour:minute:second = 21:13:50
hour:minute:second = 21:13:50
21


--------------------  mytime t1:
hour:minute:second = 07:13:20


--------------------  mydatetime da:
__str__ year--mounth--day = 2016--01--01
hour:minute:second = 21:40:50
__del__ mydatetime destroyed
>>> 

 2 多重继承

  2.1 定义父类  classA classX

class classA(object):

    def __init__(self):

        print('init action in father class A')
        
        
class classX(object):

    def __init__(self):

        print('init action in father class X')

   2.2 SubClassB 继承 classA

class SubClassB(classA):

    def __init__(self):

        print('init action in subclass B')

        super(SubClassB,self).__init__()

   2.3 SubClassC 继承 SubClassB

class SubClassC(SubClassB):

    def __init__(self):

        print('init action in subclass C')

        super(SubClassC,self).__init__()

   2.4 测试

  实例化一个SubClassC类

if __name__ == '__main__':

    print "\n"
    print "--"*10," SubClassC b:"      
    b = SubClassC()

   由输出可知:多重继承时,子类会自动调用父类的构造函数

--------------------  SubClassC b:
init action in subclass C
init action in subclass B
init action in father class A

 3 多重继承

  3.1 定义两个基本类 classA 和 classX

class classA(object):

    def __init__(self,a='classA'):
        self.a = a
        print('init action in father class A')

    def print_a(self):
        print self.a
        
class classX(object):

    def __init__(self,x='classX'):
        self.x = x
        print('init action in father class X')
    
    def print_x(self):
        print self.x

 3.2 SubClassX 类继承自 classA 和 classX

class SubClassX(classA,classX):

    def __init__(self,a='SubClassX:a',x='SubClassX:x'):

        print('init action in subclass X')

        super(SubClassX,self).__init__()
#        classX.__init__(self)
        self.a = a
        self.x = x

    def print_SubX(self):
        self.print_a()
        self.print_x()

 3.3 实例化一个 SubClassX 类

if __name__ == '__main__':
 
    print "\n"
    print "--"*10," SubClassX x:"    
    sx = SubClassX()
    sx.print_SubX()

 3.4  由输出可知 : SubClassX 继承了 classA 和 classX 的方法和属性,但不知为何只调用了 classA的初始化函数?

--------------------  SubClassX x:
init action in subclass X
init action in father class A
SubClassX:a
SubClassX:x

 3.5 分析:其实在 3.4 的输出可知,SubClassX只调用了classA的初始化,则classX的属性是不会自动初始化的,如classX的属性值x,之所以能够输出 "SubClassX:x" ,是由于在 3.2的定义中,手动初试化了 属性值。self.a = a self.x = x

如下例:

class SubClassX(classA,classX):

    def __init__(self,a='SubClassX:a',x='SubClassX:x'):

        print('init action in subclass X')

        super(SubClassX,self).__init__(a)
#        classX.__init__(self)
#        self.x = x

    def print_SubX(self):
        self.print_a()
        self.print_x()

 实例化一个 SubClassX 类:

if __name__ == '__main__':
 
    print "\n"
    print "--"*10," SubClassX x:"    
    sx = SubClassX()
    sx.print_SubX()

输出报错: 由于定义SubClassX类时,使用 super 初始化父类,只会初试化 classA,即第一个类。而classX排在了classA之后,不会自动初始化。所以在实例化类调用方法时,即 sx.print_SubX() 。由于没有属性 'x' 而报错。

--------------------  SubClassX x:
init action in subclass X
init action in father class A
SubClassX:a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "F:/Python/hysrc_py/untitled0_class.py", line 111, in <module>
    sx.print_SubX()
  File "F:/Python/hysrc_py/untitled0_class.py", line 57, in print_SubX
    self.print_x()
  File "F:/Python/hysrc_py/untitled0_class.py", line 24, in print_x
    print self.x
AttributeError: 'SubClassX' object has no attribute 'x'
>>> 

 错误提示:属性错误:'SubClassX' 对象 没有属性 'x'

posted @ 2016-09-03 23:06  .Think  阅读(16437)  评论(0编辑  收藏  举报