最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I invoke List<Class<?>> with List<Class<? extends Some>>?

programmeradmin3浏览0评论

I have to different constants,

final List<Class<? extends Some>>  CLASSES1 = List.of(...);
final List<Class<? extends Other>> CLASSES2 = List.of(...);

and a method.

static void doSome(final List<Class<?>>) classes) {
}

When I try to invoke with either CLASSES or CLASSES2, the compiler complains.

java: incompatible types:
    java.util.List<java.lang.Class<? extends Some>>
    cannot be converted to java.util.List<java.lang.Class<?>>

How can I fix this?

All I can do is this.

doSome(new ArrayList<>(CLASSES1));

Do I have other way?

I have to different constants,

final List<Class<? extends Some>>  CLASSES1 = List.of(...);
final List<Class<? extends Other>> CLASSES2 = List.of(...);

and a method.

static void doSome(final List<Class<?>>) classes) {
}

When I try to invoke with either CLASSES or CLASSES2, the compiler complains.

java: incompatible types:
    java.util.List<java.lang.Class<? extends Some>>
    cannot be converted to java.util.List<java.lang.Class<?>>

How can I fix this?

All I can do is this.

doSome(new ArrayList<>(CLASSES1));

Do I have other way?

Share Improve this question asked Mar 23 at 10:01 Jin KwonJin Kwon 22.1k18 gold badges129 silver badges210 bronze badges 2
  • What can you change? Can you change the types of the constants? Can you change the method's parameter type? – Sweeper Commented Mar 23 at 10:04
  • 1 This question is similar to: What is PECS (Producer Extends Consumer Super)?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Sören Commented Mar 23 at 12:04
Add a comment  | 

1 Answer 1

Reset to default 1

I changed the method like this,

static <T> void doSome(final List<Class<? extends T>>) classes) {
}

And it seems work.

doSome(CLASSES1);
doSome(CLASSES2);
发布评论

评论列表(0)

  1. 暂无评论