接口和抽象类继承,扩展类中的实现 [英] Interfaces and abstract class inheritance, implementation in extended classes
问题描述
在我看过的每个例子中,扩展类都实现了父类的接口。作为参考,以下示例:
In every example I've seen, extended classes implement the interfaces of their parents. For reference, the following example:
interface MyInterface{
public function foo();
public function bar();
}
abstract class MyAbstract implements MyInterface{
public function foo(){ /* stuff */ }
public function bar(){ /* stuff */ }
}
// what i usually see
class MyClass extends MyAbstract implements MyInterface{}
// what i'm curious about
class MyOtherClass extends MyAbstract{}
是否未能在子级中实现接口,这是实现的由父母,被认为是不良做法或什么?忽略孩子的实施有任何技术缺陷吗?
Is failure to implement an interface in a child, which is implemented by a parent, considered bad practice or something? Are there any technical drawbacks to omitting the implementation in the child?
推荐答案
我会认为你是在正确的道路上。在扩展已经实现
的类时,无需声明您正在实现该接口。对我来说,如果需要更改,它只是另一段代码。所以,是的,你是对的!
I would consider that you are on the right path. There is no need to declare that you are implementing the interface, when extending a class that already implements
it. For me it's just another piece of code to maintain if change is needed. So, yes, you are correct!
这篇关于接口和抽象类继承,扩展类中的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!