0

要实现的接口:

public interface Message {
}

public interface Handles<T extends Message>  {
    void hanlde(T message);
}

具体实施:

public class EnforcementHandler<T extends Handles<T extends Message>> {

}

意外的界限

这种模式在 Java 中是否可行?

4

2 回答 2

4

您必须将其写为EnforcementHandler<M extends Message, T extends Handles<M>>.

于 2020-08-07T21:39:07.353 回答
0

你的确切责任EnforcementHandler是什么,是模棱两可的。

使用其名称中的“<em>Handler”部分,向我暗示它的职责是“<em>handle”事物。所以这似乎是理解的一个合适的实现......</p>

public class EnforcementHandler<M extends Message> implements Handler<M>{ 
    
    public void handle( M msg ) { out.printf( "%1$70s%n ", msg.read( ) ); }
}

这个演示展示了如何使用它……</p>

  ...
  Message msg = ( ) -> "Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.";
  
  EnforcementHandler< Message > handler = new EnforcementHandler< >( );
  
  handler.handle( msg );
  ...

…将这条友好的信息打印给stdout…</p>

Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.
于 2020-08-08T00:56:43.037 回答