分销系统网站,茶叶网站建设一般的风格,网站体验调查问卷怎么做,室内装修公司排名Java基础10#xff08;反射#xff09;
1 什么是反射
官方解释#xff1a;反射#xff08;reflection#xff09;技术通常被用来检测和改变应用程序在 Java 虚拟机中的行为表现。它是一个相对而言比较高级的技术#xff0c;反射是一种强有力的技术特性#xff0c;因此… Java基础10反射
1 什么是反射
官方解释反射reflection技术通常被用来检测和改变应用程序在 Java 虚拟机中的行为表现。它是一个相对而言比较高级的技术反射是一种强有力的技术特性因此可以使得应用程序突破一些藩篱执行一些常规手段无法企及的目的。通俗理解就是反射是一种非常规手段能在程序运行时修改程序的行为使用有风险需谨慎。反射可以被视为动态语言反射机制允许程序再执行期间借助反射的API来获取任意类的内部信息。
反射的功能①在运行时判断对象所属类②在运行时通过构造器获取对象的实例③在运行时获取类的成员变量属性方法等④通过反射获取类中的成员属性方法。
反射涉及要的一些类
类描述 java.lang.Class 反射的源头Class类的实例表示正在运行的java应用程序的类和接口 java.lang.reflect.Field 提供有关类或接口的属性信息及动态范围权限可以理解为封装了反射类属性的类 java.lang.reflect.Method 提供有关类或接口的单独某个方法的信息 java.lang.reflect.Constructor 提供类的单个构造方法的信息以及他的访问权限
2 理解Class类实例化Class类实例
Class类是反射的源头。注意要和类的概念区分开来Class类是一个实实在在的类在 java.lang下有个Class.java的文件和我们自己定义的类一样Class对象就是这个类的实例。Class类是继承了Object的特殊类他的内部可以记录类的成员接口等信息可以理解为Class是一个用来表示类的类。
java是运行在JVM上的我们写好的类经过编译器编译后会生成.class文件。在运行期间当我们要实例化一个类时会先在内存中查看是否存在这个类存在的话就直接创建类实例如果没有的话就会就会去加载这个类当加载一个类或者类加载器的defineClass()被JVM调用便会为这个类产生一个class对象一个Class类的实例用来表示这个类该类所有实例都共同拥有这个Class对象而且是唯一的。
在java中类是用来描述信息的写明了有哪些内部的属性及接口相当于定义了一套规则。Class对象被用来对类的情况进行表述的一个实例类的实例表征可以理解为对类的图形化这样JVM才看的懂看做模板。类的实例化对象就是通过模版开辟出的一块内存进行实际的使用。
创建Class类实例
1调用运行时类本身的.class属性。如Class clazz String.class
2通过运行时类的对象。如Person p new Person(); Class c p.getClass();
3通过Class类的静态方法forName(String className)。 如 Class.forName(“包名.类名”)
4通过JVM类加载器。如ClassLoader c this.getClass().getClassLoader(); Class clazz c.getClass();
Object中有个getClass方法获得运行时类。
3 运行时创建类Class的对象并获得类的完整结构
public class TestReflect {Testpublic void test() throws Exception {Class clazz Class.forName(month201906.day13.Person);Person p (Person) clazz.newInstance();// 获得包名Package package1 clazz.getPackage();String packName package1.getName();System.out.println(packName);// 获得类名System.out.println(clazz.getSimpleName());// 获得属性名及其权限修饰符Field[] fields clazz.getDeclaredFields();for (Field field : fields) {System.out.println(Modifier.toString(field.getModifiers()) --- field.getName());}// 获得方法名及其权限修饰符Method[] methods clazz.getDeclaredMethods();for (Method method : methods) {System.out.println(Modifier.toString(method.getModifiers()) --- method.getName());}// 获得父类System.out.println(clazz.getSuperclass());// 获得注解Annotation[] annotations clazz.getAnnotations();for (Annotation annotation : annotations) {System.out.println(annotation);}// 获取接口Class[] interfaces clazz.getInterfaces();for (Class inter : interfaces) {System.out.println(inter);}}
}Target({ TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })
Retention(RetentionPolicy.RUNTIME)
interface MyAnnotation {String name() default zhujie;
}class Work {String worker;public void doWorker() {System.out.println(工作);}
}MyAnnotation(name 注解)
class Person extends Work implements Serializable {public String name;int age;private String gender;protected int score;public Person() {System.out.println(无参构造);}public Person(String name, int age) {this.name name;this.age age;}public Person(String name, int age, String gender, int score) {this.name name;this.age age;this.gender gender;this.score score;}private String Method1(String name, int age, String gender, int score) {this.name name;this.age age;this.gender gender;this.score score;return name;}public String Method2() {return Person [name name , age age , gender gender , score score ];}public static void Method3() {System.out.println(Method3 static);}private void Method4() {System.out.println(Method4 private);}protected void Method5() {System.out.println(Method5 protected);}void Method6() {System.out.println(Method6 null);}public void Method7() {System.out.println(Method6 public);}Overridepublic String toString() {return Person [name name , age age , gender gender , score score ];}}4 通过反射获取类的指定属性、方法、构造器
public class TestReflect {Testpublic void test1() throws Exception{Class clazz Class.forName(month201906.day13.Person);Person p (Person) clazz.newInstance();//获取指定构造器System.out.println(clazz.getDeclaredConstructor());//有参构造ConstructorPerson con clazz.getDeclaredConstructor(String.class,int.class,String.class,int.class);Person newInstance con.newInstance(小明,18,男,1000);System.out.println(newInstance);//获取指定属性Field name clazz.getDeclaredField(name);//调用属性name.set(p, 小红);System.out.println(p);Field gender clazz.getDeclaredField(gender);gender.setAccessible(true);gender.set(p, 女);System.out.println(p);//获取指定方法Method method1 clazz.getDeclaredMethod(Method1, String.class,int.class,String.class,int.class);//调用方法method1.setAccessible(true);method1.invoke(p, 张三,20,男,5000);System.out.println(p);//调用方法Method method4 clazz.getDeclaredMethod(Method4);method4.setAccessible(true);method4.invoke(p);}}
5 动态代理
public class TestDynamicProxy {public static void main(String[] args) {RealMothod1 real new RealMothod1();MyInvocationHandler inv1 new MyInvocationHandler(real);// 获取代理对象pubMothod1 obj (pubMothod1) inv1.getObj();obj.action();RealMothod2 r new RealMothod2();MyInvocationHandler inv2 new MyInvocationHandler(r);// 获取代理对象pubMothod2 pub (pubMothod2) inv2.getObj();pub.show();}
}// 公共方法
interface pubMothod1 {void action();
}interface pubMothod2 {void show();
}// 被代理类1
class RealMothod1 implements pubMothod1 {Overridepublic void action() {System.out.println(被代理类1号);}}// 被代理类2
class RealMothod2 implements pubMothod2 {Overridepublic void show() {System.out.println(被代理类2号);}
}// 代理类的处理类 1.创建代理对象 ,2.调用代理对象代理的方法
class MyInvocationHandler implements InvocationHandler {// 被代理类的对象的声明,代理被代理类的对象Object obj; // 给被代理对象 初始化public MyInvocationHandler(Object obj) {this.obj obj;}// 代理对象 ,代理对象调用重写的那个方法自动调用invokepublic Object getObj() {return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);}Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println(我是invoke);// 反射 被代理对象的方法Object object method.invoke(obj, args);return object;}}