4

我决定不只是盲目地复制和粘贴,而是决定了解我复制和粘贴的内容,但我被困住了R.id.class.getFields()!我最初的猜测是它会是一个静态Class变量,但是 id 类有这样的吗?

R = R 类R.java

id = 的内部 id 类R.java

类=??

获取字段() =Class.getFields()

4

2 回答 2

4

R.java是一类。从Resources. R.id正在访问内部类。public static final class id。_ R.id.class将为您提供 R.id 的 Class 对象,getFields方法将返回public该类中的所有字段R.id。看看reflection机制。

编辑:反射

于 2012-07-17T08:53:02.653 回答
1
Class Overview
The in-memory representation of a Java class. This representation serves as the starting point for querying class-related information, a process usually called "reflection". There are basically three types of Class instances: those representing real classes and interfaces, those representing primitive types, and those representing array classes.

Class instances representing object types (classes or interfaces)

These represent an ordinary class or interface as found in the class hierarchy. The name associated with these Class instances is simply the fully qualified class name of the class or interface that it represents. In addition to this human-readable name, each class is also associated by a so-called signature, which is the letter "L", followed by the class name and a semicolon (";"). The signature is what the runtime system uses internally for identifying the class (for example in a DEX file).

Classes representing primitive types

These represent the standard Java primitive types and hence share their names (for example "int" for the int primitive type). Although it is not possible to create new instances based on these Class instances, they are still useful for providing reflection information, and as the component type of array classes. There is one Class instance for each primitive type, and their signatures are:

B representing the byte primitive type
S representing the short primitive type
I representing the int primitive type
J representing the long primitive type
F representing the float primitive type
D representing the double primitive type
C representing the char primitive type
Z representing the boolean primitive type
V representing void function return values
Classes representing array classes

These represent the classes of Java arrays. There is one such Class instance per combination of array leaf component type and arity (number of dimensions). In this case, the name associated with the Class consists of one or more left square brackets (one per dimension in the array) followed by the signature of the class representing the leaf component type, which can be either an object type or a primitive type. The signature of a Class representing an array type is the same as its name. Examples of array class signatures are:

[I representing the int[] type
[Ljava/lang/String; representing the String[] type
[[[C representing the char[][][] type (three dimensions!)
于 2012-07-17T08:55:56.373 回答