“`”

<pre><code>继承概念的实现方式主要有2类:实现继承、接口继承。

实现继承是指使用基类的属性和方法而无需额外编码的能力;
接口继承是指仅使用属性和方法的名称、但是子类必须提供实现的能力(子类重构爹类方法);
python 两种类:经典类 新式类
python3 新式类 —— 都默认继承object class Animal(object): == class Animal:
python2 经典类和新式类 并存
class Animal: 经典类 —— 继承顺序 个别使用方法
class Animal(object): 新式类

继承分为单继承和多继承
Python是支持多继承的
如果没有指定基类,python的类会默认继承object类,object是所有python类的基类,它提供了一些常见方法(如__str__)的实现。
</code></pre>

 

 

补充继承的应用(面试题)

 

 

<pre><code>1、对象可以调用自己本类和父类的所有方法和属性, 先调用自己的 自己没有才调父类的。谁(对象)调用方法,方法中的self就指向谁

class Foo:
def __init__(self):
self.func()

def func(self):
print('Foo.func')

class Son(Foo):
def func(self):
print('Son.func')

s = Son()
# Son.func

========================================================
class A:
def get(self):
self.say()

def say(self):
print('AAAAA')

class B(A):
def say(self):
print('BBBBB')

b = B()
b.get() #输出结果为:BBBBB
</code></pre>

 

 

 

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.