Java反射机制获取/调用类的属性和方法

本文介绍了Java反射机制的核心功能,包括在运行时判断对象类、构造类对象、获取成员变量和方法以及动态调用方法。通过示例展示了如何使用反射调用不同参数的方法,获取声明的字段、方法和构造器。
摘要由CSDN通过智能技术生成

      Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类;在运行时构造任意一个类的对象;在运行时判断任意一个类所具有的成员变量和方法;在运行时调用任意一个对象的方法;生成动态代理。


public class Message {
	public static final String CODE_0000 = "错误码0000";
	public static final String CODE_0001 = "错误码0001";
	public static final String CODE_0002 = "错误码0002";
	public static final String CODE_0003 = "错误码0003";

	public Message(String a){
		this.getMessageMethod("hello");
	}
	public Message(){
		this.getMessageMethod("nothing");
	}
	public String getMessageMethod(String param) {
		return "this is getMessageMethod.and param is " + param;
	}

	public String getMessageMethod(String param, int i) {
		return "this is getMessageMethod.Param is " + param + "," + i;
	}
}


测试类:

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test {

	public static void main(String[] args) throws Exception {
		Class clazz=Class.forName("test.Message");// 这里写清楚包名.类名,否则会找不到
		
		System.out.println("---↓---Value-Of-\"CODE_0000\"---↓---");  
		Object obj=clazz.newInstance();
		Field fid=clazz.getField("CODE_0000");
		System.out.println(fid.get(obj));
		
		System.out.println("--↓--Use-Method-\"getMessageMethod(String)\"--↓--");  
        String methodName = "getMessageMethod";
        Method method = clazz.getMethod(methodName,String.class);
        Object  value = method.invoke(clazz.newInstance(),"hello");
        System.out.println( value.toString());  
        
		System.out.println("--↓--Use-Method-\"getMessageMethod(String,int)\"--↓--");  
        method = clazz.getMethod(methodName,String.class,int.class);
        value = method.invoke(clazz.newInstance(),"hello",2);
        System.out.println( value.toString()); 
        
        System.out.println("---↓---getDeclaredFields---↓---");  
		Field[] fields=clazz.getDeclaredFields();
		for(int i=0;i<fields.length;i++){  
	        System.out.println(fields[i].getName()+":"+fields[i].get(clazz.newInstance()));  
	    } 
          
        System.out.println("---↓---getDeclaredMethods---↓---");  
		Method[] declaredMethods=clazz.getDeclaredMethods();
		for(int i=0;i<declaredMethods.length;i++){  
	        System.out.println(declaredMethods[i].getName());  
	    } 
		System.out.println("---↓---getMethods---↓---");  
		Method[] methods=clazz.getMethods();
		for(int i=0;i<methods.length;i++){  
	        System.out.println(methods[i].getName());  
	    } 
		System.out.println("---↓---getConstructors---↓---");  
		Constructor[] constructor=clazz.getConstructors();
		for(int i=0;i<constructor.length;i++){  
	        System.out.println(constructor[i].toString());  
	    } 	
	}
}


输出:

---↓---Value-Of-"CODE_0000"---↓---
错误码0000
--↓--Use-Method-"getMessageMethod(String)"--↓--
this is getMessageMethod.and param is hello
--↓--Use-Method-"getMessageMethod(String,int)"--↓--
this is getMessageMethod.Param is hello,2
---↓---getDeclaredFields---↓---
CODE_0000:错误码0000
CODE_0001:错误码0001
CODE_0002:错误码0002
CODE_0003:错误码0003
---↓---getDeclaredMethods---↓---
getMessageMethod
getMessageMethod
---↓---getMethods---↓---
getMessageMethod
getMessageMethod
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
---↓---getConstructors---↓---
public test.Message(java.lang.String)
public test.Message()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值