Python——方法重写与构造方法

一、Python构造方法
先看一个例子,如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def init(self,num):
        self.num=num

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py==========
>>> test=A()
>>> test.init(10)
>>> test.num
10
>>> 

这种方法是在类中自行定义了一个初始化函数并对num赋值,但是对该值赋值需要在对象实例化之后,通过实例化的对象调用初始化方法。因此,就初始化操作而言显得不够灵活,可以通过构造方法解决这个问题。与普通方法不同,当一个对象创建时,在类的构造方法就会被调用,对实例化对象进行初始化。构造方法的定义和使用如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def __init__(self):
        self.num=10
        self.data=20
    def output(self):
        print(self.num,'  ',self.data)

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py==========
>>> test=A()
>>> test.output()
10    20
>>> 

二、方法的重写和重载

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def output(self):
        print('This is a test case for class A')

class B(A):
    pass

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py==========
>>> testA=A()
>>> testA.output()
This is a test case for class A
>>> testB=B()
>>> testB.output()
This is a test case for class A
>>> 

实际上在B中相当于是继承类A的output方法,所以在B中没有重写该方法的情况下,输出的结果与A一致,实现继承父类方法的重写如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def output(self):
        print('This is a test case for class A')

class B(A):
    def output(self):
        print('This is a test case for class B')       

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py==========
>>> testA=A()
>>> testA.output()
This is a test case for class A
>>> testB=B()
>>> testB.output()
This is a test case for class B
>>> 

对于子类方法的重写,就是对与父类相同的方法名进行函数体的改写,从而表现出父类与子类的不同特性。但是,对于初始化操作而言,不能直接改写,否则可能出错。

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def __init__(self):
        self.flag=1
    def write(self):
        if self.flag==1:
            self.flag=0
            print('Write file success')
        else:
            print('Write file fail')

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py========== 
>>> testA=A()
>>> testA.write()
Write file success
>>> testA.write()
Write file fail
>>> 

这个类的作用是进行文件的写操作,但是只能写一次,当第二次进行写操作时报错。若是有另外一个类继承该类,并进行了类的初始化定义,那么结果就会出错,如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def __init__(self):
        self.flag=1
    def write(self):
        if self.flag==1:
            self.flag=0
            print('Write file success')
        else:
            print('Write file fail')
class B(A):
    def __init__(self):
        self.s='Test case for class B'
    def output(self):
        print(self.s)

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py========== 
>>> testB=B()
>>> testB.output()
Test case for class B
>>> testB.write()
Traceback (most recent call last):
  File "<pyshell#40>", line 1, in <module>
    testB.write()
  File "C:\Users\Mr_Deng\Desktop\test.py", line 7, in write
    if self.flag==1:
AttributeError: 'B' object has no attribute 'flag'
>>> 

在这个例子中类B继承了父类A,但是在B中却重新定义了构造方法,这样使得B的实例对象,尽管拥有了父类A的write方法,但是由于没有flag变量的初始化,在调用父类的write方法时报错。为了解决这个问题,可以使用未绑定的超类构造方法,如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def __init__(self):
        self.flag=1
    def write(self):
        if self.flag==1:
            self.flag=0
            print('Write file success')
        else:
            print('Write file fail')
class B(A):
    def __init__(self):
        A.__init__(self)
        self.s='Test case for class B'
    def output(self):
        print(self.s)

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py========== 
>>> testB=B()
>>> testB.write()
Write file success
>>> testB.output()
Test case for class B
>>> 

在子类B中使用B.init(self)方法,将当前实例作为self参数提供给未绑定方法,使得子类也拥有了与父类初始化中相同的属性和方法。当然,也可以不使用未绑定方法而使用新版中的super方法,如下:

# _*_ coding:utf-8 _*_
__metaclass__=type
class A:
    def __init__(self):
        self.flag=1
    def write(self):
        if self.flag==1:
            self.flag=0
            print('Write file success')
        else:
            print('Write file fail')
class B(A):
    def __init__(self):
        super(B,self).__init__()
        self.s='Test case for class B'
    def output(self):
        print(self.s)

运算结果:
==========RESTART: C:\Users\Mr_Deng\Desktop\test.py========== 
>>> testB=B()
>>> testB.write()
Write file success
>>> testB.write()
Write file fail
>>> testB.output()
Test case for class B
>>> 
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值