所用实体类:
package reflect;
//定义一个学生类,作为测试项
public class Student {
private String id;
public int age;
String phone;
//无参构造方法
public Student(){}
//全参构造方法
public Student(String id, int age, String phone) {
super();
this.id = id;
this.age = age;
this.phone = phone;
}
//部分参数构造方法
public Student(String id, int age) {
super();
this.id = id;
this.age = age;
}
//私有的构造方法
private Student(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
//定义一个Public方法
public void showPublic(){
System.out.println("我是公有的方法");
}
//定义一个私有的方法
private void showPrivate(){
System.out.println("我是私有的方法");
}
//定义一个受保护的方法
protected void showProtected(){
System.out.println("我是受保护的方法");
}
//定义一个默认的方法
void show(){
System.out.println("我是默认的方法");
}
//定义一个有返回值的方法
public String showReturn(){
return "我是有返回值的方法";
}
@Override
public String toString() {
return "Student [id=" + id + ", age=" + age + ", phone=" + phone + "]";
}
}
反射操作:
package reflect;
import java.lang.reflect.Constructor;
public class TestConstructor {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//创建运行时类对象
Class<Student> student = Student.class;
//查看所有public构造器
Constructor<?>[] con = student.getConstructors(); //只能访问public
for (Constructor<?> constructor : con) {
System.out.println(constructor);
}
System.out.println("------------1--------------");
//查看所有构造器
Constructor<?>[] con1 = student.getDeclaredConstructors();
for (Constructor<?> constructor : con1) {
System.out.println(constructor);
}
System.out.println("--------------2---------------");
//获取某一特定的构造器
Constructor<Student> stu = student.getConstructor(String.class,int.class);
System.out.println(stu);
System.out.println("----------------------3------------------------");
//获取一个私有的构造器
Constructor<Student> stu1 =student.getDeclaredConstructor(String.class);
System.out.println(stu1);
System.out.println("----------------------3.1------------------------");
//获取所有构造器的修饰符、方法名、参数列表
Constructor<?>[] con2 = student.getDeclaredConstructors();
for (Constructor<?> constructor : con2) {
System.out.println("构造器为:"+constructor);
//获取构造器的访问修饰符
System.out.println("访问修饰符为:"+constructor.getModifiers());
//获取构造器的名字
System.out.println("构造器的名字为:"+constructor.getName());
//构造去的参数列表
Class<?>[] cons = constructor.getParameterTypes(); //这些对象表示此 Constructor 对象所表示构造方法的形参类型。
System.out.println("参数类型为:");
for (Class<?> class1 : cons) {
System.out.print(class1+" |||||| ");
}
/*Type[] type = constructor.getGenericParameterTypes(); //这些对象表示此 Constructor 对象所表示的方法的形参类型。
System.out.println("参数类型为:");
for (Type type2 : type) {
System.out.println(type2+" |||||| ");
}*/
System.out.println();
System.out.println("~~~~~~~~~~~~");
}
System.out.println("--------------------------4--------------------------");
//利用反射构造方法,获取对象
//获得一个具体对应参数的构造器
// Constructor<Student> s = student.getDeclaredConstructor(String.class,int.class); //public
Constructor<Student> s = student.getDeclaredConstructor(String.class); //private
//有可能访问的是非public构造方法,所以就要设置访问权限
s.setAccessible(true);
//通过这个类构造器构造一个具体的类对象出来
Student stud = s.newInstance("0001"); //声明类的新实例
System.out.println(stud);
System.out.println("--------------------------5--------------------------");
}
}