java反射方法的各种情况

package com.org.reflect;


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


import org.junit.Test;


/**
 * 反射类:方法的解析
 * @author Administrator
 *
 */
public class Ref2 {

// 反射无参的函数 public void abc()
@Test
public void test1() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getMethod("abc", null);
method.invoke(stu, null);


}


// 反射类的方法:public void abc(String name,int age)
@Test
public void test2() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
/**
* @param name
*            the name of the method
* @param parameterTypes
*            the list of parameters
*/
Method method = clazz.getMethod("abc", String.class, int.class);
method.invoke(stu, "abc", 22);


}


// 反射类的方法:public void abc(String name,int [] password)
@Test
public void test3() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getMethod("abc", String.class, int[].class);
method.invoke(stu, "abcd", new int[] { 123, 3982 });


}


// 反射类的方法:public static void abc(String name)
@Test
public void test4() throws Exception {
// Student stu=new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getMethod("abc", String.class);
/**
* 因为下面的方法是运行静态方法,所以不需要对象的实例,但是你给其传对象实例,也是可以的
*/
method.invoke(null, "jack");


}


// 反射类的方法:public static void abc(String name)
@Test
public void test5() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getMethod("abc", String.class);
method.invoke(stu, "jack");


}
// 反射类的方法:public void abc(String name,int age)
//综合利用反射解析类的构造函数,然后将该构造方法创建的对象给反射解析出来的method
@Test
public void test6() throws Exception {
//Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Constructor constructor = clazz.getConstructor(String.class);
Student stu = (Student) constructor.newInstance("jack");
Method method = clazz.getMethod("abc", String.class, int.class);
method.invoke(stu, "jack1", 22);


}
// 反射类的方法:public static void main(String[] args) {

@Test
public void test7() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
// Constructor constructor = clazz.getConstructor(String.class);
// Student stu = (Student) constructor.newInstance("jack");
Method method = clazz.getMethod("main", String [].class);
//为了兼容jdk1.4
/*由于兼容性问题,在JDK1.4没有引入可变参数Object...类型,所以使用数组来表示, 
        invoke函数接收到String数组后进行拆分,得到两个String变量,但是没有main函数接受 
                           两个String值,所以不行,但是使用Object数组就可以了*/
method.invoke(null, new Object[]{new String[]{"123"}});


}

// 反射类的方法:public static void main(String[] args) {

@Test
public void test8() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
// Constructor constructor = clazz.getConstructor(String.class);
// Student stu = (Student) constructor.newInstance("jack");
Method method = clazz.getMethod("main", String [].class);
//设置其为object对象,就不拆了,不然反射的时候遇到数组就拆
method.invoke(null, (Object)new String[]{"123"});


}
//反射类的方法:类似main方法,参数为数组的方法
@Test
public void test9() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getMethod("abc", String[].class);
method.invoke(stu, new Object[]{ new String[]{"123","234"}});
}


//反射类的方法:private void abc(String name,int age,String classNo) 
@Test
public void test10() throws Exception {
Student stu = new Student();
Class clazz = Class.forName("com.org.reflect.Student");
Method method = clazz.getDeclaredMethod("abc", String.class,int.class,String.class);
method.setAccessible(true);//暴力反射
method.invoke(stu, "zhangsan",23,"2011211111");
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值