反射相关问题及使用 (可以自己跑一下看一下结果)
1、 利用反射获取一个简单类
/*都同属于一个类class类具体可以看object类源码*/
//反射获取一个类方法一直接包名加类名
Class<?> persono = Class.forName("com.offcn.pojo.person");
System.out.println(persono);
//反射获取一个类方法二对象。getclass
person person = new person();
Class<person> persont = (Class<person>) person.getClass();
System.out.println(persont);
/*获取类的名字(包名加类名)*/
System.out.println("类名字:"+persono.getName());
/*获取类的名字(单纯类名字)*/
System.out.println("类名字:"+persono.getSimpleName());
2、 根据反射获取类的属性
/*利用反射获取类的属性(非私有属性)*/
Field[] fields = persono.getFields();
for (Field field : fields) {
System.out.println(field);
}
/*利用反射获取类的属性(全部的)*/
Field[] declaredField = persono.getDeclaredFields();
for (Field field : declaredField) {
System.out.println(field);
}
/*当然如果你知道这个类有哪些属性那你可以特意的获取这些属性的值*/
Field name = persono.getDeclaredField("name");
System.out.println(name);
3、获取类的方法
/*获取类的方法(本类和父类的公共方法)*/
Method[] methods = persono.getMethods();
for (Method method : methods) {
System.out.println("方法1:" + method);
}
/*获取类的方法(本类的方法)*/
Method[] declaredMethods = persono.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println("方法2"+method);
}
/*获取指定方法*/
Method setAge = persono.getMethod("setAge", int.class);
System.out.println("指定方法"+setAge);
4、获取构造器
/*获取全部构造器*/
Constructor[] constructors = persono.getConstructors();
for (Constructor constructor : constructors){
System.out.println("构造器:"+constructor);
}
/*获取指定构造器*/
System.out.println("指定构造器:"+persono.getConstructor(null));
真正使用运用(强大的地方)
1、构造一个对象
/*构造一个对象newInstance()调用无参构造器*/
Object o = persono.newInstance();
System.out.println(o);
/*通过有参构造器构建一个对象*/
Constructor<?> declaredConstructor = persono.getDeclaredConstructor(String.class, int.class, String.class);
System.out.println(declaredConstructor.newInstance("123", 12, "122"));
2、调用方法
/*通过反射调用方法*/
Object o1 = persono.newInstance();
Method setAge1 = persono.getDeclaredMethod("setAge", int.class);
/*激活方法(可以理解成给o1对象的setAge1方法设置1这个值)*/
setAge1.invoke(o1,1);
System.out.println(o1);
3、改变属性值
/*利用反射操作属性*/
Object o2 = persono.newInstance();
Field name1 = persono.getDeclaredField("name");
/*私有属性要关闭安全监测*/
name1.setAccessible(true);
name1.set(o2,"c++");
System.out.println(o2);
4、利用反射获取注解值
/*通过反射获得注解*/
Annotation[] annotations = persono.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println("注解:" + annotation);
}
/*获得指定注解值*/
inter annotation = persono.getAnnotation(inter.class);
System.out.println(annotation.value());
/*获取类上指定注解值*/
Field name2 = persono.getDeclaredField("name");
valu annotation1 = name2.getAnnotation(valu.class);
System.out.println(annotation1.name());
附录:实体类,注解代码附上
package com.offcn.pojo;
@inter("person")
public class person {
@valu(age=1,name="java")
private String name;
@valu(age=1,name="c++")
private int age;
@valu(age=1,name="cgo")
public String school;
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", age=" + age +
", school='" + school + '\'' +
'}';
}
public person(){}
public person(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
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 getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
package com.offcn.pojo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface valu{
int age();
String name();
}
package com.offcn.pojo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface inter {
String value();
}