gpt4 book ai didi

java - JavaPoet 的泛型类

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:12:26 24 4
gpt4 key购买 nike

嘿,我正在尝试生成这样的类:

public abstract class ResourceListAdapter<T extends ResourceViewHolder> extends RecyclerView.Adapter<T> {}

目前我可以生成:

public abstract class ResourceListAdapter extends RecyclerView.Adapter<?> {}

使用以下代码:

TypeSpec type = TypeSpec.classBuilder(thisClass)
.superclass(ParameterizedTypeName.get(adapterClassName, WildcardTypeName.subtypeOf(Object.class)))
.addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
.build();

我也可以做这样的事情:

private ResourceListAdapter<? extends ResourceViewHolder> adapter;

与:

ParameterizedTypeName.get(thisClass,WildcardTypeName.subtypeOf(resourceViewHolderClassName));

但我无法将其结合起来。那你有什么想法吗??

最佳答案

我有一个解决方案!

TypeSpec type = TypeSpec.classBuilder(thisClass)
.superclass(ParameterizedTypeName.get(adapterClassName, TypeVariableName.get("T")))
.addTypeVariable( TypeVariableName.get("T", resourceViewHolderClassName))

将生成:

public abstract class ResourceListAdapter<T extends ResourceViewHolder> extends RecyclerView.Adapter<T> {}

关于java - JavaPoet 的泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41065000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com