1.Class类
package com.imooc.reflect.test;
public class Person {
public Person() {
super();
}
public Person(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void eat(){
System.out.println("吃......");
}
}
package com.imooc.reflect.test;
import org.junit.Test;
public class ClassTest {
@Test
public void demo1() throws ClassNotFoundException {
Class class1 = Person.class;
Person person = new Person();
Class class2 = person.getClass();
Class class3 = Class.forName("com.imooc.reflect.test.Person");
}
}
2.Constructor类
package com.imooc.reflect.test;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class ConstructorTest {
@Test
public void demo1() throws Exception {
Class class1 = Class.forName("com.imooc.reflect.test.Person");
Constructor c = class1.getConstructor();
Person person = (Person)c.newInstance();
person.eat();
}
@Test
public void demo2() throws Exception {
Class class1 = Class.forName("com.imooc.reflect.test.Person");
Constructor c = class1.getConstructor(String.class, String.class);
Person person = (Person) c.newInstance("张三", "男");
System.out.println(person);
}
}
3.Field类
将Person类中的name属性设置为public,没有get,set方法,测试公有属性:
package com.imooc.reflect.test;
public class Person {
public Person() {
super();
}
public Person(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public String name;
private String sex;
public void eat(){
System.out.println("吃......");
}
@Override
public String toString() {
return "Person [name = " + name + ", sex = " + sex + "]";
}
}
@Test
public void demo1() throws Exception {
Class class1 = Class.forName("com.imooc.reflect.test.Person");
Field field = class1.getField("name");
Person p = (Person) class1.getConstructor().newInstance();
field.set(p, "李四");
Object obj = field.get(p);
System.out.println(obj);
}
测试私有属性:
@Test
public void demo2() throws Exception {
Class class2 = Class.forName("com.imooc.reflect.test.Person");
Field field1 = class2.getDeclaredField("sex");
Person p1 = (Person) class2.getConstructor().newInstance();
field1.setAccessible(true);
field1.set(p1, "男");
Object obj = field1.get(p1);
System.out.println(obj);
System.out.println(p1);
}
4.Method类
测试公有方法:
@Test
public void demo1() throws Exception {
Class class1 = Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class1.getConstructor().newInstance();
Method method = class1.getMethod("eat");
method.invoke(person);
}
在Person类中添加private方法run(),测试私有的方法:
private void run(){
System.out.println("跑......");
}
@Test
public void demo2() throws Exception {
Class class2 = Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class2.getConstructor().newInstance();
Method method = class2.getDeclaredMethod("run");
method.setAccessible(true);
method.invoke(person);
}
在Person类中添加private带参方法sayHello(),测试私有的方法带参:
public String sayHello(String name){
return "Hello " + name;
}
@Test
public void demo3() throws Exception {
Class class3 = Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class3.getConstructor().newInstance();
Method method = class3.getDeclaredMethod("sayHello", String.class);
method.setAccessible(true);
Object obj = method.invoke(person, "Tom");
System.out.println(obj);
}
|