[英]Implementing an interface without implementing it
Can implement an interface without implementing it-- as in: 可以在不实现的情况下实现接口,例如:
interface A { void m2(); }
class B { public void m2() { ... } }
class C extends B implements A { ... /* no implementation of m2() here */ }
What i'm wondering is, is there a way to force the descendant class, class C
here, to implement the method m2()
? 我想知道的是,是否有一种方法可以强制后代类(此处为类
C
实现方法m2()
?
This looks a bit sideways-- breaking the expected chain in inheritance. 这看起来有点横线-中断了预期的继承链。
There can be cases i can't think of now where it would be necessary to force the subclass in this fashion. 可能有些情况我现在想不起来,有必要以这种方式强制子类。
TIA. TIA。
//========================== // =====
EDIT: one scenario: 编辑:一种情况:
There is a class B
and an interface A
developed in two different places in the application with a method m2()
having the same signature in both. 在应用程序中的两个不同位置开发了一个类
B
和一个接口A
,两者中的方法m2()
具有相同的签名。
B
is implementing m2()
in its own way-- without even knowing what A
is all about. B
正在以自己的方式实现m2()
甚至不知道A
的全部含义。
when C
above gets quickly in, it has an m2()
that has nothing to do with the contract in A
. 当上面的
C
快速进入时,它具有m2()
与A
的合同无关。
This takes poor management of development. 这需要对开发进行不良的管理。 but, still.
但是,仍然。
//================================= // =================================
EDIT-2: EDIT 2:
Restricting m2()-of-B's access to package level or protected
works. 限制B的m2()对包级别或
protected
作品的访问。 This way, m2()
of B
that C
is inheriting won't do for that of A
since it isn't public
. 这样,
C
继承的B
m2()
不会对A
m2()
起作用,因为它不是public
。
This is a bit of tempering with what a class should do in itself-- but then it isn't. 对类本身应该做的事情进行一些调整,但实际上并非如此。 A super-class should be able to direct a sub this much.
一个超类应该能够如此直接地指导一个潜艇。 loosely similar to the use of
final
on methods or on the class itself. 大致类似于
final
方法或类本身的使用。 Note AndyThomas's useful ans. 注意AndyThomas的有用ans。 though.
虽然。
As written, class C must satisfy both the contracts for B.m2()
and A.m2()
. 按照书面规定,C类必须同时满足
B.m2()
和A.m2()
的合同。
Those contracts are either compatible or not. 这些合同是兼容的还是不兼容的。
If those contracts are compatible, then B.m2()
is either a sufficient implementation for C or not. 如果那些合同是兼容的,那么
B.m2()
要么是C的充分实现,要么不是。
If those contracts are not compatible, then C cannot both extend B and implement A. In this case, you could use composition instead of inheritance -- have C implement A, and include a B in its representation. 如果这些合同不兼容,那么C不能同时扩展B和实现A。在这种情况下,您可以使用组合而不是继承-让C实现A,并在其表示形式中包含B。
There is no mechanism in Java to force all instantiable classes implementing an interface to override superclass implementations of methods in the interface. Java中没有机制可以强制实现接口的所有可实例化类重写接口中方法的超类实现。 You can, however, force all instantiable classes to implement an
abstract
method somewhere in their inheritance hierarchy. 但是,您可以强制所有可实例化类在其继承层次结构中的某处实现
abstract
方法。
如果将类B声明为抽象类,则不需要实现m2,因此需要C(通过继承B)来执行以下操作:
abstract class B implements A {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.