奉天承运,博主诏曰:
java反射
获取Class对象的方式
public class TestClass {
public static void main(String[] args) {
try {
//第一种获取Class对象的方式
Class<?> tClass = Class.forName("cn.kgc.kb11.TestClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//用对象获取Class对象的方式
TestClass tc=new TestClass();
Class<? extends TestClass> tcClass = tc.getClass();
//直接使用类型来获取Class对象的方式
Class<TestClass> typeClass = TestClass.class;
}
}
Student类
public class Student extends Parent{
public int id;
String name;
private String gender;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
'}';
}
private Student (){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String getGender() {
return gender;
}
private void setGender(String gender) {
this.gender = gender;
}
private String show(int cnt,String good){
return "我叫:"+name+",我的学号是:"+id+",我的性别是:"+gender+",老师叫我拿"+cnt+"杯"+good;
}
}
Parent类
public class Parent {
private double money;
}
TestStudent类
import cn.kgc.kb11.Student;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class TestStudent {
public static Student getStudent() throws Exception {
Class<? extends Student> c = Student.class;
System.out.println(c.getPackage().getName());
System.out.println(c.getName());
Constructor<? extends Student> cons = c.getDeclaredConstructor();
cons.setAccessible(true);
Student o = cons.newInstance();
System.out.println(o);
return o;
}
public static void setFields()throws Exception{
Student s = getStudent();
Class<? extends Student> stuClass = s.getClass();
Field[] fields = stuClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
if (null != fields[i].getName() && "id".equals(fields[i].getName())) {
fields[i].set(s, 6);
} else if (null != fields[i].getName() && "name".equals(fields[i].getName())) {
fields[i].set(s, "王老五");
} else if (null != fields[i].getName() && "gender".equals(fields[i].getName())) {
fields[i].set(s, "男");
}
}
}
public static void main(String[] args) throws Exception {
//1.获取Class对象
Student s = getStudent();
Class<? extends Student> stuClass = s.getClass();
Method gender=stuClass.getDeclaredMethod("setGender", String.class);
gender.setAccessible(true);
gender.invoke(s,"女");
s.setId(1);
s.setName("鱼鱼酱");
System.out.println(s);
Method show = stuClass.getDeclaredMethod("show", int.class, String.class);
show.setAccessible(true);
Object o=show.invoke(s,25,"星巴克");
// int modifiers = show.getModifiers();
System.out.println(o);
Type dad = stuClass.getGenericSuperclass();
Class<?> daddy = stuClass.getSuperclass();
System.out.println(dad.getTypeName());
// Field id = stuClass.getField("id");
// id.set(s,3);
// Field gender = stuClass.getDeclaredField("gender");
// gender.setAccessible(true);
// gender.set(s,"男");
// System.out.println(s);
// Field[] fields = stuClass.getDeclaredFields();
// for (int i = 0; i < fields.length; i++) {
// fields[i].setAccessible(true);
// if (null != fields[i].getName() && "id".equals(fields[i].getName())) {
// fields[i].set(s, 6);
// } else if (null != fields[i].getName() && "name".equals(fields[i].getName())) {
// fields[i].set(s, "王老五");
// } else if (null != fields[i].getName() && "gender".equals(fields[i].getName())) {
// fields[i].set(s, "男");
// }
// }
// System.out.println(s);
// Class<? extends Student> c = Student.class;
// System.out.println(c.getPackage().getName());
// System.out.println(c.getName());
// try {
// Constructor<? extends Student> cons=c.getDeclaredConstructor();
// cons.setAccessible(true);
// Student o = cons.newInstance();
// System.out.println(o);
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (NoSuchMethodException e) {
// e.printStackTrace();
// }
}
}
钦此。