gpt4 book ai didi

Java - 有没有办法从引用类型推断对象类型

转载 作者:行者123 更新时间:2023-11-30 03:44:52 25 4
gpt4 key购买 nike

public static void printType(Object o){
type = **someWayToDeduceTheObjectType()**
fields = type.getDeclaredFields()
System.out.println("The type of the object is : " + type);
System.out.println("The fields of this type of class is : " + fields);
}

是否可以从通用引用类型推断传递对象的对象类型?

如果不是,原因是什么?

我认为这是不可能的,因为类型转换的存在,但我无法查明确切的原因。

编辑:我在这里指的不是instanceof运算符。假设我没有要比较的类型列表。

最佳答案

这很有可能 - 你只需调用 getClass() :

public static void printType(Object o){
type = o.getClass();
fields = type.getDeclaredFields()
System.out.println("The type of the object is : " + type);
System.out.println("The fields of this type of class is : " + fields);
}

关于Java - 有没有办法从引用类型推断对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995551/

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