反射有关
package com.cn;
public class Student {
public String name;
public int id;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", age=" + age + "]";
}
public Student(String name, int id, int age) {
super();
this.name = name;
this.id = id;
this.age = age;
}
public Student(String name, int id) {
super();
this.name = name;
this.id = id;
}
public Student(){
}
public void show(){
System.out.println("10 years");
}
public String display(String nationString){
System.out.println("guojia"+nationString);
return nationString;
}
private String love(String name){
return name;
}
public String dove(String obj, Integer age){
System.out.println("My age is "+age+"My name is"+obj);
return obj;
}
public static void china(){
System.out.println("woshi");
}
}
**************************************************************
package com.cn;
import static org.junit.Assert.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
@org.junit.Test
public void test() throws Exception {
Class clazz=Student.class;
Field field=clazz.getField("name");
Student student=(Student) clazz.newInstance();
System.out.println(student);
field.set(student,"Hujin");
System.out.println(student);
}
//运行时类私有属性的调用
@org.junit.Test
public void test2() throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException{
Class zlClass=Student.class;
Field age=zlClass.getDeclaredField("age");
age.setAccessible(true);
Student student=(Student) zlClass.newInstance();
age.set(student, 87);
//student.setAge(8);
System.out.println(student);
}
//运行时类方法的调用
@org.junit.Test
public void testmethod() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Class zlClass=Student.class;
//getMethod(string namemethod,Class...paras)获取运行时类的public方法
Method method=zlClass.getMethod("show");
Student student=(Student) zlClass.newInstance();
Object object=method.invoke(student);
System.out.println(object);
//调用方法,invoke(object obj,object...paras)
Method method2=zlClass.getMethod("toString");
Object object2=method2.invoke(student);
System.out.println(object2);
//静态方法的调用
Method method3=zlClass.getMethod("china");
method3.invoke(Student.class);
//有参数有返回值
Method method4=zlClass.getMethod("dove", String.class,Integer.class);
Object object3= method4.invoke(student, "My",12);
System.out.println(object3);
//调用私有方法
Method method5=zlClass.getDeclaredMethod("display", String.class);
method5.setAccessible(true);
Object nation= method5.invoke(student, "Chn");
System.out.println(nation);
//调用指定的构造器
Constructor constructor=zlClass.getDeclaredConstructor(String.class,int.class,int.class);
constructor.setAccessible(true);
Student student2=(Student) constructor.newInstance("Wang",12,1121);
System.out.println(student2);
}
}