![](/img/trans.png)
[英]How to create a generic type that only allow a specific class to extends/implements it
[英]How do you enforce that a method parameter type extends a specific class and implements a specific interface?
您如何强制方法参数类型既扩展特定类又实现特定接口?
使用&
创建联合:
<T extends Foo & Bar>
第一种类型之后的所有内容都必须是接口。
/*
A method that will only accept a parameter that :
extends SomeClass , AND implements SomeInterface .
*/
private < T extends SomeClass & SomeInterface > void someMethod ( T parameter )
{
// do something ...
}
class MyClass {}
interface MyInterface {}
public <T extends MyClass & MyInterface> void myMethod(T param) {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.