黑马程序员_JAVA反射学习

------- android培训 java培训 、期待与您交流! ---------- 
反射主要用于框架。

Person类如下:

package com.fly.reflect; import java.io.InputStream; import java.util.List; /* * Person类,用于反射构造方法 */ public class Person { public String name = "aa"; private int password = 1232323; private static int age = 19; public Person() { System.out.println("person"); } public Person(String name) { System.out.println("name:" + name); } public Person(String name, int age) { System.out.println("person name:" + name + " age:" + age); } private Person(List list) { System.out.println("list"); } private Person(int a) { System.out.println("a==" + a); } //测试用 public void testMe() { System.out.println("可以了"); } public void aa1() { System.out.println("aa1"); } public void aa1(String name, int password) { System.out.println(name + ":" + password); } public Class[] aa1(String name, int[] password) { return new Class[]{String.class}; } private void aa1(InputStream in) { System.out.println(in); } public static void aa1(int num) { System.out.println(num); } public static void main(String[] args) { System.out.println("This is main function"); } }

一、用反射获取构造方法
反射方法如下:

package com.fly.reflect; import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.List; import org.junit.Test; //反射类的构造函数,创建类的对象 public class Demo1 { //反射构造函数:public Person() @Test public void test1() throws Exception { //加载类 Class clazz = Class.forName("com.fly.reflect.Person"); //获取空参构造方法 Constructor c = clazz.getConstructor(null); //创建对象 Person p = (Person)c.newInstance(null); p.testMe(); } //反射构造函数:public Person(String name) @Test public void test2() throws Exception { Class clazz = Class.forName("com.fly.reflect.Person"); Constructor c = clazz.getConstructor(String.class); Person p = (Person)c.newInstance("gaozx"); p.testMe(); } //反射构造函数:public Person(String name, int age) @Test public void test3() throws Exception { Class clazz = Class.forName("com.fly.reflect.Person"); Constructor c = clazz.getConstructor(String.class, int.class); Person p = (Person)c.newInstance("gaozx", 12); p.testMe(); } //反射构造函数:public Person(List list) @Test public void test4() throws Exception { Class clazz = Class.forName("com.fly.reflect.Person"); //获取私有构造方法 Constructor c = clazz.getDeclaredConstructor(List.class); c.setAccessible(true); //暴力反射 Person p = (Person)c.newInstance(new ArrayList()); p.testMe(); } //getDeclaredConstrctor和getConstructor的区别 @Test public void test5() throws Exception { Class clazz = Class.forName("com.fly.reflect.Person"); Constructor c = clazz.getConstructor(String.class); Person p = (Person)c.newInstance("Sim"); p.testMe(); } }

获取公共构造方法和获取私有构造方法的方法不一样。 clazz.getDeclaredConstructor()也可以用于获取公共构造方法。


二、反射获取类的方法

package com.fly.reflect; import java.io.FileInputStream; import java.io.InputStream; import java.lang.reflect.Method; import org.junit.Test; //反射类的方法 public class Demo2 { //反射类的方法:public void aa1() @Test public void test1() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getMethod("aa1", null); method.invoke(p, null); } //反射类的方法:public void aa1(String name, int password) @Test public void test2() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getMethod("aa1", String.class, int.class); method.invoke(p, "aa", 1); } //反射类的方法:public Class[] aa1(String name, int[] password) @Test public void test3() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getMethod("aa1", String.class, int[].class); Class[] cs = (Class[]) method.invoke(p, "aa", new int[]{1, 2}); System.out.println(cs[0]); } //反射类的方法:private void aa1(InputStream in) @Test public void test4() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getDeclaredMethod("aa1", InputStream.class); method.setAccessible(true); method.invoke(p, new FileInputStream("e:\\1.txt")); } //反射类的方法:public static void aa1(int num) @Test public void test5() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getMethod("aa1", int.class); method.invoke(null, 11); } //反射类的方法:public static void main(String[] args) @Test public void test6() throws Exception { Class clazz = Class.forName("com.fly.reflect.Person"); Method method = clazz.getMethod("main", String[].class); method.invoke(null, new Object[]{new String[]{"1","2"}}); } }



三、反射获取类的字段

package com.fly.reflect; import java.lang.reflect.Field; import org.junit.Test; //反射类的字段 public class Demo3 { //反射字段:public String name = "aa"; @Test public void test1() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Field f = clazz.getField("name"); //获取字段的类型 Class type = f.getType(); //获取字段的值 Object obj = (String) f.get(p); System.out.println((String)obj); f.set(p, "bbbb"); System.out.println(p.name); } //反射字段:private int password; @Test public void test2() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Field f = clazz.getDeclaredField("password"); f.setAccessible(true); int pwd = (Integer) f.get(p); System.out.println(pwd); f.set(p, 999999); int pwd1 = (Integer) f.get(p); System.out.println(pwd1); } //反射字段:private static int age = 19 @Test public void test3() throws Exception { Person p = new Person(); Class clazz = Class.forName("com.fly.reflect.Person"); Field f = clazz.getDeclaredField("password"); f.setAccessible(true); int pwd = (Integer) f.get(p); System.out.println(pwd); f.set(p, 999999); int pwd1 = (Integer) f.get(p); System.out.println(pwd1); } }


----------------------- android培训java培训、java学习型技术博客、期待与您交流! ----------------------

详情请查看:http://edu.csdn.net/heima

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值