简体   繁体   中英

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) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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