php php抽象类扩展另一个抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7269641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php abstract class extending another abstract class
提问by Jakub M.
Is it possible in PHP, that an abstract class inherits from an abstract class?
在 PHP 中,抽象类是否可能从抽象类继承?
For example,
例如,
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(); }
}
( abstract class extends abstract class in php?does not give an answer)
(抽象类在php中继承了抽象类?没有给出答案)
回答by Arnaud Le Blanc
Yes, this is possible.
是的,这是可能的。
If a subclass does not implements all abstract methods of the abstract superclass, it must be abstract too.
如果子类没有实现抽象超类的所有抽象方法,它也必须是抽象的。
回答by matino
回答by user2806167
It will work, even if you leave the abstract function b(); in class MoreConcrete.
即使你离开抽象函数 b(); 它也会起作用。在类 MoreConcrete 中。
But in this specific example I would transform class "Generic" into an Interface, as it has no more implementation beside the method definitions.
但在这个特定的例子中,我会将类“通用”转换为接口,因为它除了方法定义之外没有更多的实现。
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(); }
}