简体   繁体   English

实现接口而不实现接口

[英]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 it is sufficient, an override is not required. 如果足够,则不需要覆盖。
  • If it is not sufficient, then it is the responsibility of class C to override it. 如果这不够,则C类有责任对其进行覆盖。
    • Inside class C, you can force an override by ... simply overriding the method. 在类C内,您可以通过...简单地覆盖方法来强制覆盖。
    • Outside class C, you cannot force C to override an existing, non-abstract superclass method. 在类C之外,您不能强制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.

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