python 中的重写一般方法和重写特殊的构造方法
2016-10-19 19:16
357 查看
在python中 有时需要进行重写,重写是继承机制中的一个重要部分, 可以重写一般方法也可以重写构造方法,构造方法是用来初始化新创建对象的状态。
class A :
def hello(self):
print('Hello,i am A.')
class B(A):
pass>>>a = A()
>>>b =B()
>>>a.hello()
Hello, i am A.
>>>b.hello()
Hello, i am A.
为什么会这样呢? 因为B类没有定义自己的hello方法,故当hello被调用时,原始信息就被打印出来了。
B类也可以重写这个hello方法。
class B(A):
def hello(self):
print('Hello,i am B.')>>>b = B()
>>>b.hello()
Hello,i am B.
以上的是重写一般方法!
——————————————————————————————————————————————————
如果特殊的构造方法被重写的话,将会导致原始基类的方法无法被调用。<pre name="code" class="python">class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print('Aaaah...')
self.hungry = False
else:
print('No,thanks!')
>>>b = Bird()
>>>b.eat()
Aaaah...
>>>b.eat()
No,thanks!
现在为子类SongBird .
class SongBird(Bird):
def __init__(self):
self.sound = 'Squawk!'
def sing(self):
print('self.sound')
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
报错,提示SongBird没有hungry特性。
如何解决这个问题呢?有两种方法:1、调用未绑定的基类构造方法 2、使用super函数
1调用未绑定的基类构造方法是怎么样呢? 让我们用实例来说明:
class SongBird(Bird):
def __init__(self):
Bird.__init__(self):
self.sound = 'Squawk!'
def sing(self):
print('self.sound')
只添加一行代码Bird.__init__(self).
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
Aaaah...
>>>sb.eat()
No.thanks!
2.使用super函数
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print('Aaaah...')
self.hungry = False
else:
print('No,thanks!')
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
Aaaah...
>>>sb.eat()
No.thanks!
=====================================END=========================================
class A :
def hello(self):
print('Hello,i am A.')
class B(A):
pass>>>a = A()
>>>b =B()
>>>a.hello()
Hello, i am A.
>>>b.hello()
Hello, i am A.
为什么会这样呢? 因为B类没有定义自己的hello方法,故当hello被调用时,原始信息就被打印出来了。
B类也可以重写这个hello方法。
class B(A):
def hello(self):
print('Hello,i am B.')>>>b = B()
>>>b.hello()
Hello,i am B.
以上的是重写一般方法!
——————————————————————————————————————————————————
如果特殊的构造方法被重写的话,将会导致原始基类的方法无法被调用。<pre name="code" class="python">class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print('Aaaah...')
self.hungry = False
else:
print('No,thanks!')
>>>b = Bird()
>>>b.eat()
Aaaah...
>>>b.eat()
No,thanks!
现在为子类SongBird .
class SongBird(Bird):
def __init__(self):
self.sound = 'Squawk!'
def sing(self):
print('self.sound')
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
报错,提示SongBird没有hungry特性。
如何解决这个问题呢?有两种方法:1、调用未绑定的基类构造方法 2、使用super函数
1调用未绑定的基类构造方法是怎么样呢? 让我们用实例来说明:
class SongBird(Bird):
def __init__(self):
Bird.__init__(self):
self.sound = 'Squawk!'
def sing(self):
print('self.sound')
只添加一行代码Bird.__init__(self).
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
Aaaah...
>>>sb.eat()
No.thanks!
2.使用super函数
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print('Aaaah...')
self.hungry = False
else:
print('No,thanks!')
class SongBird(Bird): def __init__(self): Super(SongBird,self).__init__() self.sound = 'Squawk!' def sing(self): print('self.sound')
>>>sb = SongBird()
>>>sb.sing()
Squawk!
>>>sb.eat()
Aaaah...
>>>sb.eat()
No.thanks!
=====================================END=========================================
相关文章推荐
- python中重写和特殊构造方法
- 构造一个特殊栈,实现方法getmin,返回栈中的最小元素 Python版
- 【再回首Python之美】【类-特殊方法】__str__()和__repr__()的区别和重写覆盖
- Python,构造析构函数,文档字符串方法,类的变量与对象的变量
- Python类的__getitem__和__setitem__特殊方法
- EXT 笔记 构造方法,类继承,类实例方法重写
- 【转】 python特殊方法函数
- Python(私有变量)类中的特殊方法
- python面向对象开发3 (内部类-构造-析构)魔术方法)
- Python深入01 特殊方法与多范式
- Python(私有变量)类中的特殊方法
- Python深入01 特殊方法与多范式
- Python(私有变量)类中的特殊方法
- python去除行末符EOL的一般方法
- [Python]用来定制类的特殊方法
- Python类中的特殊方法
- Python(私有变量)类中的特殊方法
- Python(私有变量)类中的特殊方法
- python 教程 第十三章、 特殊的方法
- python中的用来定制类的特殊方法的含义