本文介绍了如何在java中创建合成字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
-
如何在Java中创建合成字段?
How can synthetic fields be created in Java?
java中的合成字段只能在运行时创建吗?
如果不是:在编译时是否有符合标准的方法(不改变类文件中的某些字节)
Can synthetic fields in java only be created at runtime?If not: Is there a standard-compliant way to this at compile time (without changing some bytes in the class file)
推荐答案
当语言的奇怪需要它们时,它们是由编译器创建的。一个简单的例子是使用内部类:
They're created by the compiler when "oddities" of the language require them. A simple example of this is using an inner class:
public class Test
{
class Inner
{
}
}
Test.Inner
类将有一个合成字段来表示 Test
类的相应实例。
The Test.Inner
class will have a synthetic field to represent the appropriate instance of the Test
class.
我们可以稍微扩展此代码以显示该字段:
We can extend this code slightly to show that field:
import java.lang.reflect.*;
public class Test
{
public static void main(String[] args)
{
for (Field field : Inner.class.getDeclaredFields())
{
System.out.println(field.getName() + ": " + field.isSynthetic());
}
}
class Inner
{
}
}
使用我的编译器打印:
this$0: true
这篇关于如何在java中创建合成字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!