//留着以后参考,简单复制代码总共两个类一个Person,一个reflectDemo(用来反射Person类),出处(javaweb老方的视频)
Person类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package d.z.h.replect;
import java.util.List;
public class Person {
public String name = "ccccccc";
private int password=123;
private static int age=66;
public Person() {
System.out.println("Person()");
}
public Person(String name) {
System.out.println(name);
}
public Person(String name, int password) {
System.out.println("name="+name+":"+"password"+password);
}
private Person(List list) {
System.out.println(" Person(List list)");
}
//方法
public void getName(){
System.out.println("getName()");
}
public void getName(String name,int password){
System.out.println("name="+name+":"+"password"+password);
}
private Class[] getName(String name,int [] password){
return new Class[]{String.class};
}
public static void getName(int num){
System.out.println(num);
}
//main方法
public static void main(String[] args){
System.out.println("main!!");
}
}
|
reflectDemo类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
package d.z.h.replect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class replectDemo {
/**
* 反射就是加载类,并解剖出类的各个组成部分(Class 类提供了解剖的一些方法,能解剖出来构造函数,方法,成员变量)
*
* */
// public Person()
@Test
public void test1() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Constructor c = clazz.getConstructor(null);
Person p = (Person) c.newInstance(null);
System.out.println(p.name);
}
// public Person(String name)
@Test
public void test2() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person) c.newInstance("xxxxxxxx");
System.out.println(p.name);
}
// private Person(String name, int password)
@Test
public void test3() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Constructor c = clazz.getConstructor(String.class, int.class);
Person p = (Person) c.newInstance("dzh", 26);
System.out.println(p.name);
}
// private Person(List list)
@Test
public void test4() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Constructor c = clazz.getDeclaredConstructor(List.class);
c.setAccessible(true);// 对于私有的方法要用 “暴力反射”在外部才能被调用
Person p = (Person) c.newInstance(new ArrayList());
System.out.println(p.name);
}
/***
* 反射方法 测试
*
* @throws ClassNotFoundException
* */
// public void getName()
@Test
public void metoh1() throws Exception {
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Method metoh = clazz.getMethod("getName", null);// 1.哪个方法 2.哪个参数
metoh.invoke(p, null);// 执行方法 1.obj 对象(调用哪个对象的方法,传个对象)2.args 参数
}
// public void getName(String name,int password)
@Test
public void metoh2() throws Exception {
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Method metoh = clazz.getMethod("getName", String.class, int.class);// 1.哪个方法
// 2.哪个参数
metoh.invoke(p, "ddd", 12);// 执行方法 1.obj 对象(调用哪个对象的方法,传个对象)2.args 参数
}
// private Class[] getName(String name,int [] password)
@Test
public void metoh3() throws Exception {
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Method metoh = clazz.getDeclaredMethod("getName", String.class,
int[].class);// 1.哪个方法 2.哪个参数
metoh.setAccessible(true);
Class cs[] = (Class[]) metoh.invoke(p, "ddd", new int[] { 1, 2 });// 执行方法
// 1.obj
// 对象(调用哪个对象的方法,传个对象)2.args
// 参数
System.out.println(cs[0]);
}
// public static void getName(int num) 可以不要对象
@Test
public void metoh4() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Method metoh = clazz.getMethod("getName", int.class);
metoh.invoke(null, 21);
}
/**
* 反射main方法
* */
// public static void main(String[] args)
@Test
public void metoh5() throws Exception {
Class clazz = Class.forName("d.z.h.replect.Person");
Method metoh = clazz.getMethod("main", String[].class);
// metoh.invoke(null,new Object[]{ new String[]{"aaa","bbb"}});//jdk1.4
// 和1.5有差异 ,1.4是数组要拆开
metoh.invoke(null, (Object) new String[] { "aaa", "bbb" });// 欺骗它说不是数组
}
/**
* 反射字段
*
* */
//public String name = "ccccccc";
@Test
public void field1() throws Exception
{
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Field f = clazz.getField("name");
//获得字段的值
Object value = f.get(p);
Class type = f.getType();//获得字段类型
if(type.equals(String.class)){
String svalue = (String) value;
System.out.println(svalue);
}
//设置字段的值
f.set(p, "xxxxxxx");
System.out.println(p.name);
}
//private int password = 123;
@Test
public void field2() throws Exception
{
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Field f = clazz.getDeclaredField("password");
f.setAccessible(true);
System.out.println(f.get(p));
}
// private static int age=66
@Test
public void field3() throws Exception
{
Person p = new Person();
Class clazz = Class.forName("d.z.h.replect.Person");
Field f = clazz.getDeclaredField("age");
f.setAccessible(true);
System.out.println(f.get(p));
}
/** 三种加载类的方法 */
public void useMetoh() {
// 三种加载类的方法 之一 常用
// Class clazz = Class.forName("d.z.h.replect.Person");
// 三种加载类的方法 之二
// Class clazz = new Person().getClass();
// 三种加载类的方法 之三
Class clazz = Person.class;
}
}
|