先创建一个类,类中对属性以及方法设置了各种访问权限
public class Student {
private int id;
String name;
protected boolean sex;
public float score;
Student(){
}
public Student(int id, String name, boolean sex, float score) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.score = score;
}
public Student(int id, String name, boolean sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
protected boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
float getScore() {
return score;
}
void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", score=" + score + "]";
}
}
另一个类实现反射并对Student类中的属性以及方法进行访问操作:
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import javax.annotation.Generated;
public class GetFanshe {
public static void main(String[] args) throws Exception {
//获取反射
Student stu = new Student();
Class classobj = stu.getClass();
//获取类的修饰符 public - 1 、 private - 2 protected - 4 默认友好的 - 0
int modifiers = classobj.getModifiers();
System.out.println("类的修饰符为"+modifiers);
//获取属性的集合
Field[] fd = classobj.getDeclaredFields();
System.out.println("属性的数量"+fd.length);
for(Field f:fd)
System.out.println("修饰符:"+f.getModifiers()+" 类型:"+f.getType()+" 属性名:"+f.getName());
//通过反射访问私有属性,方式一:
Student stu1 = new Student();
Field df = classobj.getDeclaredField("id");
df.setAccessible(true);
df.set(stu1,1);
System.out.println("方式一访问私有属性:"+stu1.getId());
//方式二:
Object stu2 = classobj.newInstance();//实例化对象
Field df1 = classobj.getDeclaredField("id");
df1.setAccessible(true);
df.set(stu2,2);
Student stu3 = (Student)stu2;
System.out.println("方式二访问私有属性: "+stu3.getId());
//通过反射获取构造方法信息
Constructor[] sc = classobj.getDeclaredConstructors();//该方法返回构造器的集合
System.out.println("一共有"+sc.length+"个构造器参数如下:");
for(int i=0;i<sc.length;i++) {
Class[] pt = sc[i].getParameterTypes();//获取构造器的参数类型
System.out.print("第"+i+"个"+"构造器的参数类型是:");
if(pt.length ==0)
System.out.println("无惨构造器");
else {
for(int j=0;j<pt.length;j++)
System.out.print(pt[j].getSimpleName()+"\t");
System.out.println();
}
}
//在拿到参数类型过后可以通过反射调用构造方法
Constructor cobj = classobj.getDeclaredConstructor(int.class,String.class,boolean.class,float.class);
Object stuobj = cobj.newInstance(1001,"张三",true,320f);
Student st = (Student)stuobj;
System.out.println(st);
//通过反射获取方法
Method[] dm = classobj.getDeclaredMethods();
System.out.println("Student类中的方法如下:");
for(Method m:dm) {
System.out.print(m.getModifiers()+" ");
System.out.print(m.getName()+"(");//获得方法名字
Class[] pt = m.getParameterTypes();
for(Class t:pt)
System.out.print(t.getName()+",");
System.out.println(")");
}
//通过反射调用方法
Object s = classobj.newInstance();//先实例化对象
Method method = classobj.getDeclaredMethod("setName",String.class);
method.setAccessible(true);
Object invoke = method.invoke(s,"李四");
System.out.println("通过反射调用方法效果: "+s);
/**
*getDeclaredMethod() 获取的是类自身声明的所有方法,包含public、protected、private以及默认友好方法。
getMethod()获取的是类的所有共有方法,这就包括自身的所有public方法,和从基类继承的、从接口实现的所有public方法。
*/
}
}
程序运行结果