让Foo.class它有什么作用? [英] Foo.class what does it do?
问题描述
我寻找一些Java代码,有这个代码,我经常看到。
I m looking at some Java code, and there is this code that I see often.
Foo.class
这是用来指示类的类型?是类似于
This is being used to to indicate the Type of the class? is that similar to
Foo.GetType();
typeof(Foo);
在C#?
这是什么被用来做什么?什么的S的含义?
What is it being used for ? and what s the meaning of it?
推荐答案
是让Foo.class
在Java中等同于 typeof运算(富)
在C#。见JLS 的 15.8.2节有关的更多信息类文本的
Yes, Foo.class
in Java is equivalent to typeof(Foo)
in C#. See section 15.8.2 of the JLS for more information on class literals.
这是的不的与调用的GetType()
上的参考,从而得到的执行时间的类的一个对象。在Java相当于是 someReference.getClass()
。
It's not the same as calling GetType()
on a reference, which gets the execution time type of an object. The Java equivalent of that is someReference.getClass()
.
一个原因你可能更经常看到它在Java中代码比C#是相对于仿制药。这并不完全是稀奇事是这样的:
One reason you may see it more often in Java code than in C# is in relation to generics. It's not entirely unusual to have something like:
public class Generic<T>
{
private final Class<T> clazz;
public Generic(Class<T> clazz)
{
this.clazz = clazz;
}
}
这允许类进行反思执行时间访问等等,这是只在Java必要的,因为的类型擦除的,由此泛型类型的对象通常不能决定在执行时其类型的参数。
This allows execution-time access to the class for reflection etc. This is only necessary in Java due to type erasure, whereby an object of a generic type can't normally determine its type arguments at execution time.
这篇关于让Foo.class它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!