javase-反射方法学习记录
来源:互联网 发布:python 驼峰转 编辑:程序博客网 时间:2024/05/22 13:26
public class Demo3 {
//反射类的方法 public void test1()
@SuppressWarnings("unchecked")
@Test
public void test1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1", null);
method.invoke(clazz.newInstance(), null);
}
//public void test1(String name,int password)
@Test
public void test2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1",String.class,int.class);
method.invoke(clazz.newInstance(),"李四",1234);
}
//public Class[] test1(String name,int[] password)
@Test
public void test3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1",String.class,int[].class);
Class cs[]=(Class[]) method.invoke(clazz.newInstance(),"王五",new int[]{1,23});
System.out.println(cs[0]);
}
//private void test1(InputStream in)
@Test
public void test4() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getDeclaredMethod("test1", InputStream.class);
method.setAccessible(true);//暴力编译
method.invoke(clazz.newInstance(),new FileInputStream("d:\\1.mp4"));
}
//public static void test1(int num)
@Test
public void test5() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1", int.class);
//静态方法不用对象
method.invoke(null,23);
}
//public static void main(String[] args)
@Test
public void test6() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("main",String[].class);
//method.invoke(null,new Object[]{new String[]{"2","3"}});
method.invoke(null, (Object)new String[]{"aa","bb"});
}
}
//反射类的方法 public void test1()
@SuppressWarnings("unchecked")
@Test
public void test1() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1", null);
method.invoke(clazz.newInstance(), null);
}
//public void test1(String name,int password)
@Test
public void test2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1",String.class,int.class);
method.invoke(clazz.newInstance(),"李四",1234);
}
//public Class[] test1(String name,int[] password)
@Test
public void test3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1",String.class,int[].class);
Class cs[]=(Class[]) method.invoke(clazz.newInstance(),"王五",new int[]{1,23});
System.out.println(cs[0]);
}
//private void test1(InputStream in)
@Test
public void test4() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getDeclaredMethod("test1", InputStream.class);
method.setAccessible(true);//暴力编译
method.invoke(clazz.newInstance(),new FileInputStream("d:\\1.mp4"));
}
//public static void test1(int num)
@Test
public void test5() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("test1", int.class);
//静态方法不用对象
method.invoke(null,23);
}
//public static void main(String[] args)
@Test
public void test6() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, FileNotFoundException{
Class clazz =Class.forName("cn.reflect.Person");
Method method =clazz.getMethod("main",String[].class);
//method.invoke(null,new Object[]{new String[]{"2","3"}});
method.invoke(null, (Object)new String[]{"aa","bb"});
}
}
0 0
- javase-反射方法学习记录
- javase-反射字段学习记录
- javase-反射构造函数学习记录
- JavaSE学习笔记--反射
- JavaSE学习--反射
- javaSE之方法反射调用
- javaSE学习笔记之反射
- JavaSE学习总结-反射Reflect
- 一 javase学习记录01
- 一 javase学习记录06
- 一 javase学习记录07
- 一 javase学习记录08
- 一 javase学习记录09
- 一 javase学习记录10
- 一 javase学习记录11
- 一 javase学习记录12
- 【JavaSE学习笔记】方法
- JavaSE 学习参考:反射机制(1)
- 用(循环)(和)(递归的)”折半法“查找数
- [Leetcode]Additive Number
- 子网和子网掩码分析
- Deep Learning + Opencv + 机器视觉
- 多态的理解(父类引用指向子类对象)
- javase-反射方法学习记录
- 蓝懿 iOS 技术交流和心得分享12.2
- 解释器模式(Interpreter Pattern)
- 单例模式--(懒汉式与饿汉式)
- [leetcode]Contains Duplicate II
- Servlet学习小结(上)
- java
- Codeforces_282A
- Android设置Settings实现:PreferenceActivity【1】