简体   繁体   English

您如何强制方法参数类型扩展特定类并实现特定接口?

[英]How do you enforce that a method parameter type extends a specific class and implements a specific interface?

您如何强制方法参数类型既扩展特定类又实现特定接口?

Use & to create a union: 使用&创建联合:

<T extends Foo & Bar>

Everything after the first type must be an interface. 第一种类型之后的所有内容都必须是接口。

    /* 
        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.

相关问题 如何创建仅允许特定 class 扩展/实现它的泛型类型 - How to create a generic type that only allow a specific class to extends/implements it Java:如何强制 class 类型参数与泛型方法中指定的泛型类型相同? - Java: How do you enforce a class type parameter to be the same as generic type specified in a generic method? 如何创建具有扩展另一个类并实现接口的类型参数的基适配器? - How to create base adapter which has type parameter that extends another class and implements an interface? 如何强制将泛型类型包含在特定类中? - How can you enforce that a generic type is enclosed in a specific class? 方法参数extends class implements interface - Method argument extends class implements interface 如何确保类注释参数在编译时扩展接口? - How do you ensure a class annotation parameter extends an interface at compile time? Java:如何创建一个泛型类,它需要一个扩展类并实现接口的数据类型? - Java: how to create a generic class that requires a data type which extends a class and implements an interface? 如何通过它实现的接口在具体类中强制实现类型构造函数? - How can I enforce a type constructor in a concrete class through the interface it implements? 实现接口扩展ArrayList类 - implements interface extends ArrayList class 在java中扩展类并实现接口 - extends class and implements interface in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM