多重继承 python php
2018-04-23 本文已影响0人
任我笑笑
python是支持多重继承的
image.png
优先级 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优先于父类的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();
?>