一、常用方法
在熟悉方法前我们先建立两个类用于后面使用 Student和Person
- Student
public class Student extends Person{
public String name;
private int age;
public Student() {
name = "name";
age = 0;
}
public Student(String name, int age) {
setName(name);
setAge(age);
}
private Student(String name) {
setName(name);
setAge(-1);
}
public String getName() {
return name;
}
@Deprecated
public int getAge() {
return age;
}
private Student setName(String name) {
this.name = name;
return this;
}
private Student setAge(int age) {
this.age = age;
return this;
}
private void print(String name){
System.out.println("print "+name);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}
- Person
public class Person {
int id;
public int getId() {
return id;
}
public Person setId(int id) {
this.id = id;
return this;
}
private void run(){
System.out.println("person run");
}
}
1. 获取class的方法
Student stu = new Student();
Class stuClass = stu.getClass();
stuClass = Student.class;
stuClass = Class.forName("com.jlhlyby.java.reflex.Student");
/**所有获取方式获取的class都是相同的对象,一个类只有一个class对象*/
2.获取构造方法
//获取所有公有构造函数
Constructor[] constructors = stuClass.getConstructors();
//获取所有构造函数(包含非公有)
Constructor[] declaredConstructors = stuClass.getDeclaredConstructors();
//获取无参公有构造函数
Constructor constructor = stuClass.getConstructor();
//获取构造函数(包含非公有)
Constructor decConstructor = stuClass.getDeclaredConstructor(String.class);
//获取有参数构造函数
Constructor paramConstructor = stuClass.getConstructor(String.class,int.class);
3.构建实例
Student instance = (Student) constructor.newInstance();
Student instance1 = (Student) paramConstructor.newInstance("test",20);
//暴露访问(忽略访问权限)
decConstructor.setAccessible(true);
Student instance2 = (Student) decConstructor.newInstance("testDeclared");
4.获取字段
//所有公有字段
Field[] fields = stuClass.getFields();
//获取所有字段(包含非公有)
Field[] decFields = stuClass.getDeclaredFields();
//通过字段名获取公有字段
Field nameField = stuClass.getField("name");
//通过字段名获取字段(包含非公有)
Field ageField = stuClass.getDeclaredField("age");
ageField.setAccessible(true);
//获取某对象字段值
int age = (int) ageField.get(instance2);
//修改某对象字段值
ageField.set(instance2,2);
System.out.println("age: "+age+" -> "+ instance2.getAge());
5.获取方法
Method[] methods = stuClass.getMethods();
Method[] decMethods = stuClass.getDeclaredMethods();
Method printMethod = stuClass.getDeclaredMethod("print",String.class);
printMethod.setAccessible(true);
printMethod.invoke(stu,"test");
6.其他
//获取父类
stuClass.getSuperclass();
//获取接口
stuClass.getInterfaces();
//获取注解
stuClass.getAnnotations();
nameField.getAnnotations();
7.输出下上面的结果
System.out.println("class: "+stuClass.getName());
System.out.println();
for (Constructor cons:constructors) {
System.out.println("cons: "+cons);
}
System.out.println();
for (Constructor cons:declaredConstructors) {
System.out.println("declaredCons: "+cons);
}
System.out.println();
System.out.println("constructor: "+constructor);
System.out.println("decConstructor: "+constructor);
System.out.println("paramConstructor param: "+paramConstructor.getParameterTypes());
System.out.println("instance: "+instance);
System.out.println("instance1: "+instance1);
System.out.println("instance2: "+instance2);
System.out.println();
for (Field field : fields) {
System.out.println("field: "+field);
}
System.out.println();
for (Field decField : decFields) {
System.out.println("field: "+decField);
}
System.out.println();
System.out.println("nameField: "+nameField);
System.out.println("ageField: "+ageField);
System.out.println();
for (Method m : methods){
System.out.println("method: "+m);
}
for (Method m:decMethods){
System.out.println("decMethods: "+m);
}
写文章不易,路过的伙伴辛苦点个赞,谢谢支持!
网友评论