2个类文件
1:
public class Dog {
private String name;
private int age;
private String color;
public Dog() {
}
public Dog(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
private void set(){
System.out.println("set");
}
protected void get(){
System.out.println("get");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
}
2:
import org.junit.Test;
import java.lang.reflect.*;
public class ReflectionDemo {
/**
* 获取class对象的三种形式
*/
@Test
public void test1(){
//通过对象的getClass()方法
Dog dog = new Dog("东德牧羊犬", 5, "black");
Class aclass = dog.getClass();
//通过类.class
Class dogClass = Dog.class;
//通过Class.forName方法
try {
Class aClass1 = Class.forName("com.okme.reflection.Dog");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 通过反射来实例化对象
*/
@Test
public void test2(){
Class<Dog> dogClass = Dog.class;
try {
//通过Class对象实例化类对象,调用了默认无参构造
Dog dog = (Dog) dogClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 获取多有构造方法
*/
@Test
public void test3(){
Class<Dog> dogClass = Dog.class;
Constructor<?>[] constructors = dogClass.getConstructors();
for (int i = 0; i<constructors.length; i++){
System.out.println(constructors[i].getName());
System.out.println(constructors[i].getParameterCount());
}
try {
//获取一个指定的构造方法
Constructor<Dog> constructor = dogClass.getConstructor(String.class, int.class, String.class);
//调用带参数的构造器来实例化对象
Dog dog = constructor.newInstance("阿旺", 10, "red");
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
/**
* 获取所有的属性
*/
@Test
public void test4(){
Class<Dog> dogClass = Dog.class;
//获取非私有属性
Field[] fields = dogClass.getFields();
System.out.println(fields.length);
//获取所有包括私有属性
Field[] declaredFields = dogClass.getDeclaredFields();
System.out.println(declaredFields.length);
for (int i = 0; i < declaredFields.length; i++) {
int modifiers = declaredFields[i].getModifiers();
System.out.println(Modifier.toString(modifiers)+""+declaredFields[i].getType()+" "+declaredFields[i].getName());
}
}
@Test
public void test5(){
Dog dog = new Dog("旺旺", 6, "blue");
Class<Dog> dogClass = Dog.class;
//获取类的包名
Package aPackage = dogClass.getPackage();
System.out.println(aPackage.getName());
//获取公共的方法,包括继承的共有方法
Method[] methods = dogClass.getMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i]);
//调用公有方法
if (methods[i].getName().equals("toString")){
try {
String s = (String) methods[i].invoke(dog);
System.out.println(s);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
System.out.println("---------------------------------------------------");
//访问私有方法,获取到本类中定义的所有方法,不包括父类。
Method[] declaredMethods = dogClass.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
System.out.println(declaredMethods[i]);
if (declaredMethods[i].getName().equals("set")){
//设置私有方法可以被访问(去除访问修饰符)
declaredMethods[i].setAccessible(true);
try {
declaredMethods[i].invoke(dog);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}