简体   繁体   English

Java中的getClass()实际返回了什么?

[英]What does getClass() in Java actually return?

I am a C++ programmer currently picking up Java. 我是一名目前正在学习Java的C ++程序员。 I'm having trouble with digesting the getClass() method in Java, and I would greatly appreciate any insights you may have to offer. 我在使用Java消化getClass()方法时遇到了麻烦,我非常感谢您提供的任何见解。 From my understanding, C++'s dynamic_cast and Java's getClass() are loosely related in the sense that they are different means to the same end. 根据我的理解, C ++的dynamic_cast和Java的getClass()在相同的意义上是松散相关的,因为它们是同一目的的不同手段。 In C++, I could use dynamic_cast to determine polymorphic types, whereas in Java, I could use getClass() to determine an object's most derived class type. 在C ++中,我可以使用dynamic_cast来确定多态类型,而在Java中,我可以使用getClass()来确定对象的派生类派生类型。 With a dynamic_cast, I can get back a pointer; 使用dynamic_cast,我可以找回指针; with getClass(), I get back a class, but what does that return type (class) actually represent? 使用getClass(),我得到一个类,但是返回类型(类) 实际上代表什么? Is it a pointer to the class? 它是指向班级的指针吗?

From my understanding, C++'s dynamic_cast and Java's getClass() are loosely related 根据我的理解,C ++的dynamic_cast和Java的getClass()松散相关

C++ typeid operator would provide for better comparison. C ++ typeid运算符可以提供更好的比较。 The major difference is that in C++ typeid requires a class to have at least one virtual function, while getClass in Java can be called on objects of any type without restrictions. 主要的区别在于,在C ++中, typeid要求一个类至少有一个虚函数,而Java中的getClass可以在没有限制的任何类型的对象上调用。

what does that return type (class) actually represent? 返回类型(类)实际代表什么?

It represents a Class object suitable for doing reflection on the class - something that has no direct counterpart in C++. 它表示一个适合在类上进行反射Class对象 - 在C ++中没有直接对应的东西。 For example, you can determine all interfaces the class implements, the base class of the class, list all methods, list all fields, call methods by name, and so on. 例如,您可以确定该类实现的所有接口,类的基类,列出所有方法,列出所有字段,按名称调用方法,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM