class类和Field类的一般应用
public static void main(String[] args) {
Student zhangsan = new Student();
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<?>[] 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();
}
}
public class Test01 {
public static void main(String[] args) {
Student student=new Student();
System.out.println(student.getAge());
Class<? extends Student> clazz=student.getClass();
try {
Field field=clazz.getDeclaredField("age");
field.setAccessible(true);
field.setInt(student, 30);
field.setAccessible(false);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println(student.getAge());
Field[] fields=clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println(fields[i].getName());
}
}
}
public class Student {
private int age=20;
public String name;
public String sex;
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;
}
}
class类和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;
@NumAnnotation(numannotation=15)
private Student zhaoliu;
public Clazz(){
zhujie();
}
public void zhujie(){
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);
field.setAccessible(false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}else if(annotation==null){
NumAnnotation numannotation=field.getAnnotation(NumAnnotation.class);
field.setAccessible(true);
Student student1=new Student();
student1.setNum(numannotation.numannotation());
try {
field.set(this, student1);
field.setAccessible(false);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
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;
}
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String nameannotation() default "张三";
String sexannotation();
int ageannotation();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NumAnnotation {
int numannotation();
}
public class Student {
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
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;
}
}
public class Test {
public static void main(String[] args) {
Clazz clazz=new Clazz();
System.out.println(clazz.getZhangsan().getName());
System.out.println(clazz.getZhaoliu().getNum());
}
}