被反射的类:
@Table("tb_student")
public class Student {
@Fields(columnName="id",type="int",length=10)
private int id;
@Fields(columnName="studentName",type="varchar",length=10)
private String studentName;
@Fields(columnName="age",type="int",length=3)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(int id, String studentName, int age) {
super();
this.id = id;
this.studentName = studentName;
this.age = age;
}
public Student() {
}
反射获取类的信息:
@SuppressWarnings("all") //压制警告
public class Deam {
public static void main(String[] args) throws NoSuchFieldException, SecurityException, NoSuchMethodException {
Class c;
try {
c = Class.forName("cn.sxt.in.Student");
//获取类名
System.out.println(c.getName()); //获取包名+类名
System.out.println(c.getSimpleName()); //只获取类名
//获取属性
Field[] fields=c.getFields(); //返回属性数组,只能获取公开属性
System.out.println(fields.length);
Field[] fields2=c.getDeclaredFields(); //获得所有类型的属性
for(Field e:fields2)
{
System.out.println("aa");
System.out.println(e);
}
System.out.println(fields2.length);
Field f=c.getDeclaredField("studentName");
System.out.println(f);
//获取方法
Method[] method =c.getMethods(); //获取公开方法
Method[] method2=c.getDeclaredMethods(); //获取所有方法
for(Method mm:method2)
{
System.out.println(mm);
}
Method m=c.getDeclaredMethod("getStudentName", null); //第二个为参数类型,没有为null
Method m2=c.getDeclaredMethod("setStudentName", String.class); //方法的参数类型是String
//获取构造器
Constructor[] co=c.getDeclaredConstructors();
for(Constructor cc:co)
{
System.out.println(cc);
}
Constructor coo=c.getDeclaredConstructor(null); //获取无参构造器
System.out.println(coo);
Constructor coo2=c.getDeclaredConstructor(int.class,String.class,int.class);//获取指定有参构造器
System.out.println(coo2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
通过反射动态操作方法属性:
public class play {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
{
try {
Class c=Class.forName("cn.sxt.in.Student");
//Class<Student> c=(Class<Student>)Class.forName("cn.sxt.in.Student");后面操作不用强转
//通过反射调用构造方法,构造对象
Student s=(Student)c.newInstance(); //实际上调用了User的无参构造方法
System.out.println(s);
//指定构造对象
Constructor<Student> cc=c.getDeclaredConstructor(int.class,String.class,int.class); //获得构造器方法
Student s2=cc.newInstance(001,"me",19); //传入实际参数生成对象
System.out.println(s2.getStudentName());
//反射调用普通方法
Student s3=(Student) c.newInstance();
Method method=c.getDeclaredMethod("setStudentName", String.class);
method.invoke(s3, "mee"); //激活方法,第一个参数为对象,第二个参数为传入信息,相当于s3.setStudentName(String s);
System.out.println(s3.getStudentName());
//通过反射操作属性
Student s4=(Student)c.newInstance();
Field f=c.getDeclaredField("studentName"); //获得属性
// f.set(s4,"yyy"); //设置属性信息,第一个参数为设定的对象,第二个为实际信息,相当于s4.set(String s);
// System.out.println(s4.getStudentName()); //因为是私有属性,不能直接调用
//访问私有属性
Student s5=(Student) c.newInstance();
Field ff=c.getDeclaredField("studentName");
ff.setAccessible(true); //不需安全检查,可直接访问私有属性,其他类型也有该方法(类等)
ff.set(s5, "ada");
System.out.println(s5.getStudentName());
System.out.println(ff.get(s5)); //直接通过属性对象调用
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。