1、反射获取对象 Class.forName("地址")
public static void reflectNewInstance() {
try {
Class<?> c1=Class.forName("reflectdemo1.Student");//通过反射获取对象
Student student= (Student) c1.newInstance();//在类外实例化对象——>newInstance()
//返回值是T类型 此时T为Student
System.out.println(student);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
2、反射获取构造方法 getDeclaredConstructor(类型.class,类型.class)
//通过反射获取私有的构造方法
public static void reflectPrivateConstructor(){
try {
Class<?> c1=Class.forName("reflectdemo1.Student");//先通过反射获取对象
Constructor<?> constructor= c1.getDeclaredConstructor(String.class,int.class);//getDeclaredConstructor(类型.class,类型.class)获取与参数类型匹配的构造方法
constructor.setAccessible(true);//改变私有访问权限(重点),置为true才能访问私有
Student student=(Student)constructor.newInstance("ly",20);//此时可以根据私有的构造方法创建实例
System.out.println(student);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
3、反射获取私有属性 getDeclaredField("属性名")
//反射私有的属性 (公开的也可获取)
public static void reflectPrivateField(){
try {
Class<?> c1=Class.forName("reflectdemo1.Student");//通过反射获取对象
Student student=(Student)c1.newInstance();//实例化对象
Field field=c1.getDeclaredField("name");//通过对象获取属性getDeclaredField("属性名")
field.setAccessible(true);
field.set(student,"xyh");//修改值通过set(要修改的对象,"修改之后的值")
System.out.println(student);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
4、反射获取方法 getDeclaredMethod(“方法名称”,类型.class)
//反射私有方法
public static void reflectPrivateMethod(){
try {
Class<?> c1=Class.forName("reflectdemo1.Student");//反射对象
Student student=(Student) c1.newInstance();//创建实例
Method method=c1.getDeclaredMethod("function",String.class);//getDeclaredMethod(“方法名称”,类型.class)
method.setAccessible(true);
method.invoke(student,"私有的方法");//调用私有的方法用invoke(要修改的对象,“所要修改的值”)
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
class Student{
//私有属性name
private String name = "bit";
//公有属性age
public int age = 18;
//不带参数的构造方法
public Student(){
System.out.println("Student()");
}
private Student(String name,int age) {
this.name = name;
this.age = age;
System.out.println("Student(String,name)");
}
private void eat(){
System.out.println("i am eat");
}
public void sleep(){
System.out.println("i am pig");
}
private void function(String str) {
System.out.println(str);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}