註解
反射
註解
//創建註解 必須聲明 @Target (註解的類型 ) @Retention(註解範圍)
package com.lingzhuo.test
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()
}
package com.lingzhuo.test;
import java.lang.reflect.Field;
public class Clazz {
private int num;
@MyAnnotation(ageAnnotation = 5, sexAnnotation = "男")
private Student1 zhangsan;
@MyAnnotation(ageAnnotation = 5, sexAnnotation = "男", nameAnnotation = "李四")
private Student1 lisi;
@MyAnnotation(ageAnnotation = 5, sexAnnotation = "女", nameAnnotation = "王五")
private Student1 wangwu;
private Student1 zhaoliu;
public Clazz() {
shujie();
}
public void shujie() {
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) {
try {
field.setAccessible(true);
Student1 student = new Student1();
student.setAge(annotation.ageAnnotation());
student.setName(annotation.nameAnnotation());
student.setSex(annotation.sexAnnotation());
field.set(this, student);
field.setAccessible(false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Student1 getZhangsan() {
return zhangsan;
}
public void setZhangsan(Student1 zhangsan) {
this.zhangsan = zhangsan;
}
public Student1 getLisi() {
return lisi;
}
public void setLisi(Student1 lisi) {
this.lisi = lisi;
}
public Student1 getWangwu() {
return wangwu;
}
public void setWangwu(Student1 wangwu) {
this.wangwu = wangwu;
}
public Student1 getZhaoliu() {
return zhaoliu;
}
public void setZhaoliu(Student1 zhaoliu) {
this.zhaoliu = zhaoliu;
}
}
package com.lingzhuo.test;
public class Student1 {
private int age;
private String name;
private String sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = 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;
}
}
package com.lingzhuo.test;
public class Test {
public static void main(String[] args) {
Clazz clazz=new Clazz();
System.out.println(clazz.getZhangsan().getName());
}
}
反射
package com.lingzhuo.test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Student_Test {
public static void main(String[] args) {
Student zhangsan = new Student();
System.out.println(zhangsan.getAge());
System.out.println(zhangsan.getClass());
System.out.println(zhangsan.getClass().getName());
Class<? extends Student> clazz = Student.class;
Constructor constructor;
try {
constructor = clazz.getDeclaredConstructor();
System.out.println("得到的無參構造器:"+constructor.getName());
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
}
try {
Class[] conArg = new Class[2];
conArg[0] = Integer.class;
conArg[1] = String.class;
Constructor constructor1 = clazz.getDeclaredConstructor(conArg);
System.out.println("得到的有參構造器:" + constructor1.toString());
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
}
try {
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].getName());
Class[] parameter = methods[i].getParameterTypes();
for (int j = 0; j < parameter.length; j++) {
System.out.println(parameter[j].getName());
}
}
Field field = clazz.getDeclaredField("age");
field.setAccessible(true);
field.setInt(zhangsan, 30);
field.setAccessible(false);
System.out.println(zhangsan.getAge());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i].getName());
}
Constructor<?>[] constructor2 = clazz.getDeclaredConstructors();
for (int i = 0; i < constructor2.length; i++) {
System.out.println(constructor2[i].getName());
Class[] parameter = constructor2[i].getParameterTypes();
for (int j = 0; j < parameter.length; j++) {
System.out.println(parameter[j].getName());
}
}
}
}
package com.lingzhuo.test;
public class Student {
private int age = 20;
private String name;
private String sex;
public Student() {
}
public Student(Integer age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return 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;
}
}