简体   繁体   中英

Method parameter must be obj of certain class that implements certain interface

How to declare a method with parameter which must be same time a certain class which implements certain interface without declaring a special class for this?
Assume i have declared class ImageX and also declared interface Tagging. So I need to create some other class or method which has to receive only instances of ImageX which implements Tagging interface. How to declare such method? something like

private void someMethod (ImageX<Tagging> obj){} 

but this is not correct of course. Yes I could check the obj if it implements needed interface but I want that check in parameters. And what if it is not some method but a constructor...
am I asking about generics maybe?

您可以使方法通用,并且只使用类型交集接受同时属于ImageX和标记的对象:

private <T extends ImageX & Tagging> void someMethod (T obj){} 

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