反射获取类属性和方法的调用

package io.stream;

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

public class ReflectTest {
	public static void main(String[] args) {
		// test1();
		// test2();
		// test3();
		// test4();
		// test5();
		// test6();
		test7();
	}

	private static void test7() {
		// 得到并调用指定的方法(带参数)
		Class<?> cl;
		try {
			cl = Class.forName("io.stream.Student");
			Object obj = cl.newInstance();
			Class<?>[] paras={String.class,int.class}; 
			Method me=cl.getDeclaredMethod("sayHello", paras);//得到方法
			Object[] val={"dd",2};//把参数存进数组,参数数组为Object类型
			Object objval = me.invoke(obj, val);//执行带参数的方法
			System.out.println(objval);//输出返回值
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test6() {
		// 得到并调用指定的方法
		try {
			Class<?> cl = Class.forName("io.stream.Student");
			Object obj = cl.newInstance();
			Method me = cl.getMethod("sayHi", null);// 得到方法
			Object objval = me.invoke(obj, null);// 执行方法,普通方法执行用invoke
			System.out.println(objval);// 得到执行方法的返回值
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test5() {
		// 得到所有的方法
		try {
			Class<?> cl = Class.forName("io.stream.Student");
			Method[] me = cl.getDeclaredMethods();
			for (Method m : me) {
				System.out.println(m.getName() + "\t" + m.getTypeParameters()
						+ "\t" + m.getReturnType().getName() + "\t"
						+ Modifier.toString(m.getModifiers()));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test4() {
		// 得到并调用带参的构造方法
		try {
			Class<?> cl = Class.forName("io.stream.Student");
			Constructor<?> c = cl.getDeclaredConstructor(String.class,
					String.class, int.class);//得到构造方法
			Object obj = c.newInstance("01", "王安石", 40);//构造方法执行用newInstance方法
			// System.out.println("ff");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test3() {
		// 得到并调用无参构造方法
		try {
			Class<?> cl = Class.forName("io.stream.Student");
			Constructor<?>[] con = cl.getDeclaredConstructors();
			System.out.println(con.length);
			Constructor<?> c = cl.getDeclaredConstructor(null);// 得到无参构造方法
			Object obj = c.newInstance(null);// 新建一个实例
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test2() {
		// TODO Auto-generated method stub
		Class cl;
		try {
			cl = Class.forName("io.stream.Student");
			Object obj = cl.newInstance();
			Field f = cl.getDeclaredField("stuName");
			f.setAccessible(true);//设置私有的可以访问
			f.set(obj, "李商隐");
			System.out.println(f.get(obj));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void test1() {
		// TODO Auto-generated method stub
		try {
			Class<?> cl = Class.forName("io.stream.Student");
			// Object obj=cl.getInterfaces();
			Field[] fl = cl.getDeclaredFields();//得到属性(字段)
			for (Field f : fl) {
				System.out.println(f.getName() + "\t" + f.getType() + "\t"
						+ f.getModifiers() + "\t"
						+ Modifier.toString(f.getModifiers()));
			}
			Field f = cl.getDeclaredField("stuid");
			System.out.println(f.getName() + "\t" + f.getType() + "\t"
					+ f.getModifiers() + "\t"
					+ Modifier.toString(f.getModifiers()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值