通过反射获取运行时类的完整结构
一、补充完善上篇提供的丰富的类
代码语言:javascript复制//作为一个父类
public class Person implements Serializable {
//属性
private int age;
public String name;
//方法
private void eat(){
System.out.println("Person---eat");
}
public void sleep(){
System.out.println("Person---sleep");
}
}
package com.lanson.test03;
//Student作为子类
@MyAnnotation(value="hello")
public class Student extends Person implements MyInterface{
//属性:
private int sno;//学号
double height;//身高
protected double weight;//体重
public double score;//成绩
//方法:
@MyAnnotation(value="himethod")
public String showInfo(){
return "我是一名三好学生";
}
public String showInfo(int a,int b){
return "重载方法====我是一名三好学生";
}
private void work(){
System.out.println("我以后会找工作--》成为码农 程序员 程序猿 程序媛");
}
void happy(){
System.out.println("做人最重要的就是开心每一天");
}
protected int getSno(){
return sno;
}
//构造器
public Student(){
System.out.println("空参构造器");
}
private Student(int sno){
this.sno = sno;
}
Student(int sno,double weight){
this.sno = sno;
this.weight = weight;
}
protected Student(int sno,double height,double weight){
this.sno = sno;
}
@Override
@MyAnnotation(value="hellomyMethod")
public void myMethod() {
System.out.println("我重写了myMethod方法。。");
}
@Override
public String toString() {
return "Student{"
"sno=" sno
", height=" height
", weight=" weight
", score=" score
'}';
}
}
package com.lanson.test03;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
/*
@Target:定义当前注解能够修饰程序中的哪些元素
@Retention:定义注解的声明周期
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();//属性
}
package com.lanson.test03;
public interface MyInterface {//自定义的接口
//随便定义一个抽象方法:
void myMethod();
}
二、获取构造器和创建对象
代码语言:javascript复制package com.lanson.test03;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test01 {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取字节码信息:
Class cls = Student.class;
//通过字节码信息可以获取构造器:
//getConstructors只能获取当前运行时类的被public修饰的构造器
Constructor[] c1 = cls.getConstructors();
for(Constructor c:c1){
System.out.println(c);
}
System.out.println("-------------------");
//getDeclaredConstructors:获取运行时类的全部修饰符的构造器
Constructor[] c2 = cls.getDeclaredConstructors();
for(Constructor c:c2){
System.out.println(c);
}
System.out.println("-------------------");
//获取指定的构造器:
//得到空构造器
Constructor con1 = cls.getConstructor();
System.out.println(con1);
//得到两个参数的有参构造器:
Constructor con2 = cls.getConstructor(double.class, double.class);
System.out.println(con2);
//得到一个参数的有参构造器:并且是private修饰的
Constructor con3 = cls.getDeclaredConstructor(int.class);
System.out.println(con3);
//有了构造器以后我就可以创建对象:
Object o1 = con1.newInstance();
System.out.println(o1);
Object o2 = con2.newInstance(180.5, 170.6);
System.out.println(o2);
}
}
三、获取属性和对属性进行赋值
代码语言:javascript复制package com.lanson.test03;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class Test02 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
//获取运行时类的字节码信息:
Class cls = Student.class;
//获取属性:
//getFields:获取运行时类和父类中被public修饰的属性
Field[] fields = cls.getFields();
for(Field f:fields){
System.out.println(f);
}
System.out.println("---------------------");
//getDeclaredFields:获取运行时类中的所有属性
Field[] declaredFields = cls.getDeclaredFields();
for(Field f:declaredFields){
System.out.println(f);
}
System.out.println("---------------------");
//获取指定的属性:
Field score = cls.getField("score");
System.out.println(score);
Field sno = cls.getDeclaredField("sno");
System.out.println(sno);
System.out.println("---------------------");
//属性的具体结构:
//获取修饰符
/*int modifiers = sno.getModifiers();
System.out.println(modifiers);
System.out.println(Modifier.toString(modifiers));*/
System.out.println(Modifier.toString(sno.getModifiers()));
//获取属性的数据类型:
Class clazz = sno.getType();
System.out.println(clazz.getName());
//获取属性的名字:
String name = sno.getName();
System.out.println(name);
System.out.println("-------------------------------");
//给属性赋值:(给属性设置值,必须要有对象)
Field sco = cls.getField("score");
Object obj = cls.newInstance();
sco.set(obj,98);//给obj这个对象的score属性设置具体的值,这个值为98
System.out.println(obj);
}
}
四、获取方法和调用方法
代码语言:javascript复制package com.lanson.test03;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Test03 {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
//获取字节码信息:
Class cls = Student.class;
//获取方法:
//getMethods:获取运行时类的方法还有所有父类中的方法(被public修饰)
Method[] methods = cls.getMethods();
for(Method m:methods){
System.out.println(m);
}
System.out.println("-----------------------");
//getDeclaredMethods:获取运行时类中的所有方法:
Method[] declaredMethods = cls.getDeclaredMethods();
for(Method m:declaredMethods){
System.out.println(m);
}
System.out.println("-----------------------");
//获取指定的方法:
Method showInfo1 = cls.getMethod("showInfo");
System.out.println(showInfo1);
Method showInfo2 = cls.getMethod("showInfo", int.class, int.class);
System.out.println(showInfo2);
Method work = cls.getDeclaredMethod("work",int.class);
System.out.println(work);
System.out.println("-----------------------");
//获取方法的具体结构:
/*
@注解
修饰符 返回值类型 方法名(参数列表) throws XXXXX{}
*/
//名字:
System.out.println(work.getName());
//修饰符:
int modifiers = work.getModifiers();
System.out.println(Modifier.toString(modifiers));
//返回值:
System.out.println(work.getReturnType());
//参数列表:
Class[] parameterTypes = work.getParameterTypes();
for(Class c:parameterTypes){
System.out.println(c);
}
//获取注解:
Method myMethod = cls.getMethod("myMethod");
Annotation[] annotations = myMethod.getAnnotations();
for(Annotation a:annotations){
System.out.println(a);
}
//获取异常:
Class[] exceptionTypes = myMethod.getExceptionTypes();
for(Class c:exceptionTypes){
System.out.println(c);
}
//调用方法:
Object o = cls.newInstance();
myMethod.invoke(o);//调用o对象的mymethod方法
System.out.println(showInfo2.invoke(o,12,45));;
}
}
五、获取类的接口,所在包,注解
代码语言:javascript复制package com.lanson.test03;
import java.lang.annotation.Annotation;
public class Test04 {
public static void main(String[] args) {
//获取字节码信息:
Class cls = Student.class;
//获取运行时类的接口:
Class[] interfaces = cls.getInterfaces();
for(Class c:interfaces){
System.out.println(c);
}
//得到父类的接口:
//先得到父类的字节码信息:
Class superclass = cls.getSuperclass();
//得到接口:
Class[] interfaces1 = superclass.getInterfaces();
for(Class c:interfaces1){
System.out.println(c);
}
//获取运行时类所在的包:
Package aPackage = cls.getPackage();
System.out.println(aPackage);
System.out.println(aPackage.getName());
//获取运行类的注解:
Annotation[] annotations = cls.getAnnotations();
for(Annotation a:annotations){
System.out.println(a);
}
}
}
六、关于反射的面试题
问题1:创建Person的对象,以后用new Person()创建,还是用反射创建?
问题2:反射是否破坏了面向对象的封装性?