在Java中,什么是一个参考Class.class办?是一个、Java、class、Class

2023-09-07 11:58:43 作者:泪了眼睛熬了心

我要建一个小的Andr​​oid应用程序,而这更是一个Java的问题不是机器人的问题。翻翻教程有看起来像行:

I'm building a small Android application, but this is more of a Java question than an android question. Looking through the tutorials there are lines that look like:

startService(new Intent(this, MyService.class));

究竟是什么的MyService.class现场重新present?是不是只是一个引用类的模板?

what exactly does the "MyService.class" field represent? Is that just a reference to the class for a template?

感谢。

推荐答案

安迪的回答肯定是正确的,但我想扩大的点了一点。

Andy's answer is definitely correct, but I want to expand on the point a little.

的.class 是获得类对象的实例的特殊语法。它可以当只有类型是可用的和相关的目的的任何实例是围绕被使用。它可以用于与任何具体类型的名称,包括数组和原语。例如, int.class 是有效的。

.class is a special syntax for obtaining an instance of a Class object. It can be used when only the type is available and no instance of the related object is around. It can be used with any concrete type name, including arrays and primitives. For instance, int.class is valid.

此方式(和其他方式),以获得一个Class对象都记录在旧的阳光反射API文档。

This way (and other ways) to get a Class object are documented in the old Sun reflection API docs.

特殊的.class 语法经常出现在习惯用法中通用的code类型的标记。与的.class 文字得到了一类被称为一种令牌时方法中通过沟通双方编译时和运行时类型信息(