反射
部分方法说明
getDeclaredMethods()获取对象中定义的所有的方法名称,返回值为数组类型
getParameterTypes()获取对象方法所有形参的类型,返回值为数组类型
getDeclaredField()获取对象中所有声明的标识符的名称,返回值为数组类型
setAccessible()返回值为boolean类型,按照结果表示是否可以修改对象中的参数数值
Field 得到参数的名称
Method 得到方法的名字
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test {
public static void main(String[] args) {
Student zhangsan = new Student();
System.out.println(zhangsan.getAge());
Class<? extends Student> clazz = zhangsan.getClass();
// Class<? extends Student> clazz = Student.class;
try {
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
Class[] para = methods[i].getParameterTypes();
for (int j = 0; j < para.length; j++) {
System.out.println(para[j].getName());
}
}
Field field = clazz.getDeclaredField("age");
field.setAccessible(true);
field.setInt(zhangsan, 25);
field.setAccessible(false);
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(zhangsan.getAge());
}
}
public class Student {
private int age = 20;
private String name;
private String sex;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
运行结果:
注解
注解的使用
1.创建注解 必须声明 @Target (注解的类型)
@Retention (注解的范围)
public class Student {
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
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 MyAnnotation {
String nameAnnotation() default "张三";
String sexAnnotation();
int ageAnnotation();
}
import java.lang.reflect.Field;
public class Clazz {
@MyAnnotation(ageAnnotation = 10,sexAnnotation = "男")
private Student zhangsan;
@MyAnnotation(ageAnnotation = 10,sexAnnotation = "男",nameAnnotation = "李四")
private Student lisi;
@MyAnnotation(ageAnnotation = 10,sexAnnotation = "男",nameAnnotation = "王五")
private Student wangwu;
private Student zhaoliu = new Student();
public Clazz(){
zhujie();
}
public void zhujie() {
// TODO Auto-generated method stub
Class<? extends Clazz> cla = Clazz.class;
Field[] fields = cla.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
MyAnnotation annotation = field.getAnnotation(MyAnnotation.class);
if(annotation != null){
field.setAccessible(true);
Student student = new Student();
student.setAge(annotation.ageAnnotation());
student.setName(annotation.nameAnnotation());
student.setSex(annotation.sexAnnotation());
try {
field.set(this, student);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(false);
}
}
System.out.println(this.getZhangsan().getName());
}
public Student getZhangsan() {
return zhangsan;
}
public void setZhangsan(Student zhangsan) {
this.zhangsan = zhangsan;
}
public Student getLisi() {
return lisi;
}
public void setLisi(Student lisi) {
this.lisi = lisi;
}
public Student getWangwu() {
return wangwu;
}
public void setWangwu(Student wangwu) {
this.wangwu = wangwu;
}
public Student getZhaoliu() {
return zhaoliu;
}
public void setZhaoliu(Student zhaoliu) {
this.zhaoliu = zhaoliu;
}
}
public class Test {
public static void main(String[] args) {
Clazz clazz = new Clazz();
System.out.println(clazz.getLisi().getAge());
}
}
运行结果: