0

我在 org.jfree.data.time.RegularTimePeriod 类中看到了一些合成字段,但不知道它们的用途和用途。我使用此代码找出它们:

for (Field f : RegularTimePeriod.class.getDeclaredFields())
    if (f.isSynthetic()) System.out.println(f);

它会给出这些:

static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$java$util$Date
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$java$util$TimeZone
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Year
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Quarter
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Month
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Day
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Hour
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Minute
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Second
static java.lang.Class org.jfree.data.time.RegularTimePeriod.class$org$jfree$data$time$Millisecond

任何机构有任何想法?我只是好奇:) 谢谢。

4

1 回答 1

1

我所知道的,synthetic members are only meant to be accessed by trusted code generated by the compiler, not haphazardly by reflection.

编译器综合某些隐藏字段和方法以实现名称范围。除非另有说明,否则这些字段是私有的,或者它们最多属于包范围。

指向最外层封闭实例的合成字段名为this$0。下一个最外层的封闭实例是this$1,依此类推。(在任何给定的内部类中最多需要一个这样的字段。)包含常量副本的合成字段v被命名为val$v。这些字段是final.

所有这些合成字段都由构造函数参数初始化,这些参数与它们初始化的字段名称相同。如果其中一个参数是最内层的封闭实例,则它是第一个。所有这些构造函数参数都被认为是合成的。如果编译器确定合成字段的值只在构造函数的代码中使用,它可能会省略字段本身,只使用参数来实现变量引用。

授予对私有成员或构造函数的访问权限的非私有最终合成方法的名称形式为 access$N,其中 N 是十进制数字。这种访问协议的组织是未指定的。

我希望这有帮助。

干杯

于 2013-05-13T08:30:09.120 回答