Class类、反射
public class Student {
private int age=20;
public String name;
public String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String string) {
this.sex = string;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
// TODO Auto-generated method stub
return age;
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ClassTest {
public static void main(String[] args) {
Student zhangsan=new Student();
System.out.println(zhangsan.getAge());
Class<? extends Student> clasz=zhangsan.getClass();
try {
Method[] method=clasz.getDeclaredMethods();
for (int i = 0; i < method.length; i++) {
System.out.println("方法:"+method[i].getName());
Class[] Paraments=method[i].getParameterTypes();
for (int j = 0; j < Paraments.length; j++) {
System.out.println("参数:"+Paraments[j].getName());
}
}
Field field=clasz.getDeclaredField("age");
field.setAccessible(true);
field.set(zhangsan, 30);
System.out.println(zhangsan.getAge());
field.setAccessible(false);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (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();
}
try {
System.out.println(clasz.getDeclaredField("age").getName());
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Field[] field=clasz.getFields();
for (int i = 0; i < field.length; i++) {
System.out.println(field[i].getName());
}
}
}
运行结果:
20
方法:getName
方法:setName
参数:java.lang.String
方法:getAge
方法:getSex
方法:setSex
参数:java.lang.String
方法:setAge
参数:int
30
age
name
sex
Annotation功能注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.lang.model.element.Element;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//这是注解
String nameAnnotation() default "张三";
String sexAnntation();
int ageAnntation();
}
public class Student {
private int age=20;
public String name;
public String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String string) {
this.sex = string;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
// TODO Auto-generated method stub
return age;
}
}
public class Clasz {
@MyAnnotation(ageAnntation=20, sexAnntation = "男")
public Student zhangsan;
@MyAnnotation(ageAnntation=20, sexAnntation = "男",nameAnnotation="李四")
public Student lisi;
@MyAnnotation(ageAnntation=20, sexAnntation = "男",nameAnnotation="王五")
public Student wangwu;
public Student zhaoliu;
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;
}
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class MyAnntationTest {
public static void main(String[] args) {
Clasz clasz=new Clasz();
Class<? extends Clasz> cla=clasz.getClass();
Field[] fields=cla.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field=fields[i];
MyAnnotation anntation=field.getAnnotation(MyAnnotation.class);
if (anntation!=null) {
try {
field.setAccessible(true);
Student student=new Student();
student.setAge(anntation.ageAnntation());
student.setName(anntation.nameAnnotation());
student.setSex(anntation.sexAnntation());
field.set(clasz, student);
field.setAccessible(false);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(clasz.zhangsan.getName());
}
}
运行结果:张三