繁体   English   中英

php抽象类扩展另一个抽象类

[英]php abstract class extending another abstract class

在PHP中,抽象类是否可以从抽象类继承?

例如,

abstract class Generic {
    abstract public function a();
    abstract public function b();
}

abstract class MoreConcrete extends Generic {
    public function a() { do_stuff(); }
    abstract public function b(); // I want this not to be implemented here...
}

class VeryConcrete extends MoreConcrete {
    public function b() { do_stuff(); }

}

抽象类在php中扩展抽象类?没有给出答案)

是的,这是可能的。

如果子类没有实现抽象超类的所有抽象方法,那么它也必须是抽象的。

是的,但如果您调用$VeryConcreteObject->b()则代码将无效

这里有更详细的解释。

它会工作,即使你离开抽象函数b(); 在类MoreConcrete中。

但是在这个特定的例子中,我将类“Generic”转换为一个接口,因为除了方法定义之外它没有更多的实现。

interface Generic {
    public function a(); 
    public function b();
}

abstract class MoreConcrete implements Generic {
    public function a() { do_stuff(); }
    // can be left out, as the class is defined abstract
    // abstract public function b();
}

class VeryConcrete extends MoreConcrete {
    // this class has to implement the method b() as it is not abstract.
    public function b() { do_stuff(); }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM