Java 创建一个通用 class 实现另一个带有约束的通用接口
Java creating a generic class that implement another generic interface with constraints
要实现的接口:
public interface Message {
}
public interface Handles<T extends Message> {
void hanlde(T message);
}
具体实现:
public class EnforcementHandler<T extends Handles<T extends Message>> {
}
意外绑定
这种模式在 Java 中可能吗?
必须写成EnforcementHandler<M extends Message, T extends Handles<M>>
。
你的 EnforcementHandler
的确切责任是什么,是模棱两可的。
根据其名称的“Handler”部分,我认为它的职责是“handle”。所以这似乎是对 my 理解的适当实施…
public class EnforcementHandler<M extends Message> implements Handler<M>{
public void handle( M msg ) { out.printf( "%1s%n ", msg.read( ) ); }
}
This demo shows how it could be used…
...
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
…
Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.
要实现的接口:
public interface Message {
}
public interface Handles<T extends Message> {
void hanlde(T message);
}
具体实现:
public class EnforcementHandler<T extends Handles<T extends Message>> {
}
意外绑定
这种模式在 Java 中可能吗?
必须写成EnforcementHandler<M extends Message, T extends Handles<M>>
。
你的 EnforcementHandler
的确切责任是什么,是模棱两可的。
根据其名称的“Handler”部分,我认为它的职责是“handle”。所以这似乎是对 my 理解的适当实施…
public class EnforcementHandler<M extends Message> implements Handler<M>{
public void handle( M msg ) { out.printf( "%1s%n ", msg.read( ) ); }
}
This demo shows how it could be used…
...
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
…
Friends of space, how are you all? Have you eaten yet? Come visit us if you have time.