The reflection of Java language (二)
来源:互联网 发布:平安科技 企业大数据 编辑:程序博客网 时间:2024/05/19 14:55
具体请点击:http://www.verydemo.com/demo_c89_i106414.html
The reflection of Java language (2)
Java的反射机制主要提供以下功能:
- 在运行时判断任意一个对象所属的类
- 在运行时构造任意一个类的对象
- 在运行时判断任意一个类所具有的成员变量和方法
- 在运行时调用任意一个对象的方法
- 生成动态代理
在JDK中,主要由以下类来实现Java的反射机制,除了Class类其余类都在java.lang.reflect.*这个包中:
- Class:代表一个类
- Method:代表类的成员方法
- Field:代表类得成员变量
- Constructor:代表类的构造方法
- Array:提供了动态创建数组,以及访问数组元素的方法
以下为示例程序,DumpMethods演示了Reflection API的基本作用,它读取命令行自动参数的类名,然后打印这个类所具有的方法信息:
import java.lang.reflect.*;public class DumpMethods {public static void main(String args[])throws Exception{Class classType = Class.forName(args[0]);Method methods[] = classType.getDeclaredMethods();for(int i = 0; i < methods.length; i++){System.out.println(methods[i].toString());}}}
运行命令行:java DumpMethods java.util.Stack就会得到Stack类所具有的方法,程序打印如下结果:
public synchronized java.lang.Object java.util.Stack.pop()public java.lang.Object java.util.Stack.push(java.lang.Object)public boolean java.util.Stack.empty()public synchronized java.lang.Object java.util.Stack.peek()public synchronized int java.util.Stack.search(java.lang.Object)
0 0
- The reflection of Java language (二)
- The Histroy of language
- The language of learning
- Java Reflection(二):Classes
- Java Reflection(二):Classes
- The BookList of Programming Language
- The Art of Assembly Language
- The key of C Language
- 反射规则 the law of reflection
- Java Reflection(二):Classes
- The view of the C language
- The view of the C++ language
- Code in the Language of the Domain
- The origins of the English language
- The Development of the C Language
- Remove all the EventHandlers of the object by reflection
- Remove all the EventHandlers of the object by reflection
- 汇编(The art of assembly language)
- Tamper Data
- C语言中的sizeof关键字
- 文档与文件比较工具简单汇总
- 数组中最小的K个数
- [SQL开发小技巧]一行拆分为多行
- The reflection of Java language (二)
- DevExpress Report 使用简要说明
- C++的四种显示类型转换
- PHP empty() 的判断
- 用 PHPRPC 实现 Ajax 级联下拉菜单
- 【雷电】源码分析(三)-- 游戏背景
- javascript 中的继承
- ios如何建立和调用静态库
- Python中采用命名管道来做多进程通信