博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java反思reflect 分析Object物
阅读量:6702 次
发布时间:2019-06-25

本文共 4247 字,大约阅读时间需要 14 分钟。

直接看它的一个例子

package reflect;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;class Behavior {	private long distance;	private long amount;	public long getDistance() {		return distance;	}	public void setDistance(long distance) {		this.distance = distance;	}	public long getAmount() {		return amount;	}	public void setAmount(long amount) {		this.amount = amount;	}	public String walk() {		return "走了" + distance + "公里";	}	public String eat() {		return "吃了" + amount + "碗饭";	}}class Person extends Behavior {	private String username;	private int age;	static char sex;	protected final double high = 0;	public String[] hobby;	public Behavior be;	public String getUsername() {		return username;	}	public void setUsername(String username) {		this.username = username;	}	public int getAge() {		return age;	}	public void setAge(int age) {		this.age = age;	}	public char getSex() {		return sex;	}	public static void setSex(char sex) {		Person.sex = sex;	}	public double getHigh() {		return high;	}	public String[] getHobby() {		return hobby;	}	public void setHobby(String[] hobby) {		this.hobby = hobby;	}	public Behavior getBe() {		return be;	}	public void setBe(Behavior be) {		this.be = be;	}	public Person() {	}	public Person(String username, int age, String[] hobby, Behavior be) {		super();		this.username = username;		this.age = age;		this.hobby = hobby;		this.be = be;	}	public String fun2(int a, int b, int c, String d, double e, float f, char g) {		return a + b + c + d + e + f + g;	}}public class ObjectAnalyzer {	public void printFields(Field[] fields) {		for (int i = 0; i < fields.length; i++) {			Field f = fields[i];			int m = f.getModifiers();			String name = f.getName();			Class

> clazz = f.getType(); System.out.println(Modifier.toString(m) + " " + clazz.getName() + " " + name); } } public void printConstructor(Constructor<?>[] c) { for (int i = 0; i < c.length; i++) { Constructor<?

> con = c[i]; String name = con.getName(); int m = con.getModifiers(); Class<?>[] clazz = con.getParameterTypes(); System.out.print(Modifier.toString(m) + " " + name + "("); for (int j = 0; j < clazz.length; j++) { System.out.print(clazz[j].getName() + " "); } System.out.println(")"); } } public void printMethod(Method[] methods) { for (int i = 0; i < methods.length; i++) { Method m = methods[i]; int modi = m.getModifiers(); String name = m.getName(); Class<?> returnType = m.getReturnType(); Class<?>[] paramType = m.getParameterTypes(); System.out.print(Modifier.toString(modi) + " " + returnType.getName() + " " + name + "("); for (int j = 0; j < paramType.length; j++) { System.out.print(paramType[j].getName() + " "); } System.out.println(")"); } } public void analyzer(Object obj) { Class<?> clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); Constructor<?>[] c = clazz.getDeclaredConstructors(); Method[] method = clazz.getDeclaredMethods(); //Method[] method = clazz.getMethods(); System.out.println("=====属性变量的分析====="); System.out.println("Fields长度:" + fields.length + " 变量例如以下"); printFields(fields); System.out.println("=====构造方法分析====="); printConstructor(c); System.out.println("=====方法分析====="); printMethod(method); } public static void main(String[] args) { // TODO Auto-generated method stub Behavior be = new Behavior(); Person p = new Person("chiwei", 26, new String[] { "1", "2" }, be); new ObjectAnalyzer().analyzer(p); } }

执行结果例如以下:

=====属性变量的分析=====

Fields长度:6 变量例如以下
private java.lang.String username
private int age
static char sex
protected final double high
public [Ljava.lang.String; hobby
public reflect.Behavior be
=====构造方法分析=====
public reflect.Person()
public reflect.Person(java.lang.String int [Ljava.lang.String; reflect.Behavior )
=====方法分析=====
public void setUsername(java.lang.String )
public int getAge()
public void setAge(int )
public char getSex()
public static void setSex(char )
public double getHigh()
public [Ljava.lang.String; getHobby()
public void setHobby([Ljava.lang.String; )
public reflect.Behavior getBe()
public void setBe(reflect.Behavior )
public java.lang.String fun2(int int int java.lang.String double float char )
public java.lang.String getUsername()

版权声明:本文博主原创文章,博客,未经同意不得转载。

你可能感兴趣的文章
img图片自适应宽和高[转]
查看>>
Android Studio体验(一)--Window版本安装
查看>>
ubuntu install express
查看>>
js中substr与substring的差别
查看>>
微软职位内部推荐-Senior Software Engineer
查看>>
FusionCharts简单教程(一)---建立第一个FusionCharts图形
查看>>
sql中实现split()功能
查看>>
ZOJ 2562 More Divisors(高合成数)
查看>>
[原]Android打包之跨平台打包
查看>>
C++的try_catch异常
查看>>
(转)思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,......
查看>>
HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版
查看>>
获取MSSQL Server中的相关信息(视图、存储过程、触发器、表)
查看>>
信号处理过程中的几种常见傅里叶相关的变换
查看>>
[原创] Win7全自动精简批处理_绝对原创,绝对给力_感谢无忧给了我一年的潜水...
查看>>
2017 年热门编程语言排行榜,你的语言上榜没?
查看>>
poi 合并单元格、设置边框
查看>>
Hibernate延迟加载与opensessioninviewFilter
查看>>
Atitit 图像处理 调用opencv 通过java api attilax总结
查看>>
服务管理--systemctl命令
查看>>