python是支持多重繼承的
image.png
優(yōu)先級 https://www.zhihu.com/question/54855335
從左到右,深度遍歷负甸。
這個例子可能更好些 https://blog.csdn.net/caiknife/article/details/8579851
class A(object):
def __init__(self):
super(A, self).__init__()
print("A!")
class B(object):
def __init__(self):
super(B, self).__init__()
print("B!")
class AB(A, B):
def __init__(self):
super(AB, self).__init__()
print("AB!")
class C(object):
def __init__(self):
super(C, self).__init__()
print("C!" )
class D(object):
def __init__(self):
super(D, self).__init__()
print("D!")
class CD(C, D):
def __init__(self):
super(CD, self).__init__()
print("CD!")
class ABCD(AB, CD):
def __init__(self):
super(ABCD, self).__init__()
print( "ABCD!" )
ABCD()
php雖然不支持多重繼承绩社,但是有新加的trait
定義上跟class完全一樣屡限,用的時候在子類內use就可以喷户,并且func優(yōu)先于父類的func
http://php.net/manual/zh/language.oop5.traits.php
<?php
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
class MyHelloWorld extends Base {
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>