简体   繁体   English

什么是对象的类属性指向-MyClass.class

[英]What is class property of an object point to - MyClass.class

In java, what is the need/use of ".class" property of an object. 在Java中,需要/使用对象的“ .class”属性是什么。 eg, MyClass.class. 例如,MyClass.class。 What does .class point to. .class指向什么。

An instance of the class Class . Class的实例。

http://javadocs.org/class http://javadocs.org/class

Sometimes you need some typing information , eg when you do some reflection . 有时您需要一些打字信息 ,例如进行反思时

The .class is specially handled by the compiler and is interpreted as a Class instance. .class由编译器专门处理,并解释为Class实例。

If you've used C# before this is the equivalent of the typeof operator. 如果您之前使用过C#,则相当于typeof运算符。

eg if you want to dynamically get the full name of a type you can do: 例如,如果要动态获取类型的全名,可以执行以下操作:

System.out.println(String.class.getName());

It can be useful in some circumstances where you want to say 'any object of a particular class'. 在某些情况下,如果您想说“特定类的任何对象”,它可能会很有用。 For example, in JUnit you use the class object of a class to indicate that a test is expected to throw an exception of a particular class. 例如,在JUnit中,您使用类的类对象来指示期望测试抛出特定类的异常。

They can also be useful in some advanced uses of generics, to ensure that methods are applicable only to objects of a particular type. 它们在泛型的某些高级用法中也很有用,以确保方法仅适用于特定类型的对象。 For example, Spring HttpMessageConverter objects use them. 例如,Spring HttpMessageConverter对象使用它们。

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

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