java Method isSynthetic
来源:互联网 发布:网络系统培训 编辑:程序博客网 时间:2024/06/16 00:16
先看代码:
package com.reflect;import java.lang.reflect.Method;import junit.framework.TestCase;import static java.lang.System.out;public class TestSynthetic extends TestCase {public void testSynthetic() {try {Method[] methods = User.class.getDeclaredMethods();for (Method method : methods) {out.println(method.toString() + ", " + method.isSynthetic());}} catch (SecurityException e) {e.printStackTrace();}}class User {private int age;private String name;private User() {}private User(int age, String name) {this.age = age;this.name = name;}private int getAge() {return age;}private void setAge(int age) {this.age = age;}private String getName() {return name;}private void setName(String name) {this.name = name;}}}
运行testSynthetic,输出:
private java.lang.String com.reflect.TestSynthetic$User.getName(), falseprivate void com.reflect.TestSynthetic$User.setName(java.lang.String), falseprivate int com.reflect.TestSynthetic$User.getAge(), falseprivate void com.reflect.TestSynthetic$User.setAge(int), false
修改测试代码:
package com.reflect;import java.lang.reflect.Method;import junit.framework.TestCase;import static java.lang.System.out;public class TestSynthetic extends TestCase {public void testSynthetic() {try {User user = new User();user.age = 1;Method[] methods = User.class.getDeclaredMethods();for (Method method : methods) {out.println(method.toString() + ", " + method.isSynthetic());}} catch (SecurityException e) {e.printStackTrace();}}class User {private int age;private String name;private User() {}private User(int age, String name) {this.age = age;this.name = name;}private int getAge() {return age;}private void setAge(int age) {this.age = age;}private String getName() {return name;}private void setName(String name) {this.name = name;}}}
运行:
private java.lang.String com.reflect.TestSynthetic$User.getName(), falseprivate void com.reflect.TestSynthetic$User.setName(java.lang.String), falsestatic void com.reflect.TestSynthetic$User.access$1(com.reflect.TestSynthetic$User,int), trueprivate int com.reflect.TestSynthetic$User.getAge(), falseprivate void com.reflect.TestSynthetic$User.setAge(int), false
发现增加了一个方法:
static void com.reflect.TestSynthetic$User.access$1(com.reflect.TestSynthetic$User,int), true
这是编译器为这个类增加的,因为User这个内部类,全部的变量和方法,都是private的,但是外部程序要去调用,所以编译器做了这个工作。
上面只是修改了测试代码,并没有修改User这个内部类,就可以增加User的方法。
0 0
- java Method isSynthetic
- Java - Method Overloading vs Method Overriding
- java synchronized static method and common method
- java Native Method初涉
- Java Remote Method Invocation
- java Native Method初涉
- java Native Method初涉
- java Native Method
- java Native Method初涉
- Java Reflection 之 Method
- java Method类试用
- java Native Method初涉
- java.lang.reflect.Method
- java Native Method初涉
- java native method
- Java Native Method introduction
- java Native Method初涉
- java thread.join method
- 关于LINQ的半知半解
- Linux守护进程的编程实现
- STM32F0DISCOVERY Cortex-M0 开发板
- linux守护进程的几个关键地方
- Java的基本数据类型详解
- java Method isSynthetic
- 嵌入式学习步骤
- win7,win8,32位,64位下vs2012如何永久配置opencv2.4.6
- 对线性表(例如整形数组的各种排序)
- poj 3624 Charm Bracelet 背包
- python中的构造函数和析构函数
- 上帝的晨光03
- (十二)动态语言扩展
- win8下安装 usb blaster