黑马程序员_JAVA反射学习

来源:互联网 发布:网络搞笑猛虎下山图 编辑:程序博客网 时间:2024/05/16 04:33
------- 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()@Testpublic 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)@Testpublic 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)@Testpublic 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)@Testpublic 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的区别@Testpublic 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()@Testpublic 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)@Testpublic 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)@Testpublic 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)@Testpublic 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)@Testpublic 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)@Testpublic 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";@Testpublic 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;@Testpublic 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@Testpublic 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
原创粉丝点击