gpt4 book ai didi

java - 为什么外部类在泛型类型名称中出现两次?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:50 25 4
gpt4 key购买 nike

<分区>

问:为什么包含类的名字出现了两次?

上下文:我正在生成代码,目标是获取字段的声明,如源代码中所写(完全限定没问题,但我需要类型参数):test.Foo.Bar<java.lang.String>

package test;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

public class Foo
{
public static class Bar<TYPE> {}

private Bar<String> bar;

public static void main(String[] args) throws Exception
{
Field field = Foo.class.getDeclaredField("bar");
Type genericType = field.getGenericType();
Type type = field.getType();

System.out.println("genericType: " + genericType.getTypeName());
System.out.println(" type: " + type.getTypeName());
}
}

输出:

genericType: test.Foo.test.Foo$Bar<java.lang.String>
type: test.Foo$Bar

更新:谢谢大家的意见。由于这个问题被标记为重复,我发布了我当前的工作解决方案 over there .

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