gpt4 book ai didi

java - 检查Java中持有对象的引用的类名

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

class A {
}

class B extends A {
}

class TestType {
public static void main(String args[]) {
A a = new B();
// I wish to use reference 'a' to check the Reference-Type which is 'A'.
}
}

有可能吗?如果不是,请说明原因。

最佳答案

Chris Jester-Young's comment很棒。它说:

You can't. The static type of local variables is not retained in the bytecode, nor at runtime. (If it's a field, you can use reflection on the field's containing class to get the field's type.)

另见 What is the concept of erasure in generics in Java?

http://gafter.blogspot.com/search?q=super+type+token .

关于java - 检查Java中持有对象的引用的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12703285/

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