哪里有做ppt模板下载网站,怎么在建设银行网站购买国债,聚美优品网站模版,微信公众平台申请注册入口一#xff1a;反射的理解
Reflection#xff08;反射#xff09;是指被视为动态语言的关键#xff0c;反射机制允许程序在执行期借助于Reflection API 取得任何类的内部信息#xff0c;并直接操作任意对象的内部属性及方法。
框架反射注解设计模式
二#xff1a;体会反…一反射的理解
Reflection反射是指被视为动态语言的关键反射机制允许程序在执行期借助于Reflection API 取得任何类的内部信息并直接操作任意对象的内部属性及方法。
框架反射注解设计模式
二体会反射机制的“动态性”
// 创建一个指定类的对象classpath指定类的全类名
public Object getInstance(String classpath) throws Exception {Class clazz Class.forName(classpath);return clazz.newInstance();
}Test
public void test2() {Random random new Random();String classpath;for (int i 0; i 100; i) {int number random.nextInt(3);switch (number) {case 0:classpath java.util.Date;break;case 1:classpath java.lang.Object;break;case 2:classpath com.cn.test.Person;break;}try {Object objgetInstance(classpath);System.out.println(obj);} catch (Exception e) {e.printStackTrace();}}
}
三反射机制能提供的功能
》在运行时判断任意一个对象所属的类
》在运行时构造任意一个类的对象
》在运行时判断任意一个类所具有的成员变量和方法
》在运行时获取泛型信息
》在运行时调用任意一个对象的成员变量和方法
》在运行时处理注解
》生成动态代理
四相关API
java.lang.Class :反射的源头
java.lang.reflect.Method
java.lang.reflect.Field
java.lang.reflect.Constructor
五关于Class类的理解
1.类的加载过程
程序经过javac.exe命令以后会生成一个或多个字节码文件(.class结尾)。
接着我们使用java.exe 命令 对某个字节码文件进行解释运行。相当于将某个字节码文件加载到内存中在此过程就称为类的加载。加载到内存中的类我们称为运行时类此运行时类就作为Class 的一个实例。
2.换句话说Class的实例 对应着 一个运行时类。
3.加载到内存中的运行时类会缓存一定的时间在此时间之内我们可以通过不同方式来获取此运行时类。
4.获取Class实例的几种方式
Test
public void test3() throws ClassNotFoundException {//1.调用类.class 属性,获取Class实例Class class1 Person.class;System.out.println(class1);Person p new Person();//2.调用getClass(),获取Class实例Class class2 p.getClass();System.out.println(class2);//3.Class.forName,获取Class实例 Class class3 Class.forName(com.cn.test.Person);System.out.println(class3);//4.调用类的加载器getClassLoader(),获取Class实例ClassLoader classLoader Reflection1Test.class.getClassLoader();Class class4 classLoader.loadClass(com.cn.test.Person);System.out.println(class4);//System.out.println(class1class2);
5. 总结:创建类的对象的方式
方式一 new 构造器
方式二要创建Xxx类的对象可以考虑Xxx/Xxxs 类是否有静态方法存在可以调用其静态方法创建Xxx对象。
方式三通过反射
6.Class实例可以是哪些结构的说明
1class外部类成员成员内部类静态内部类局部内部类匿名内部类
2interface:接口
3) []:数组
4enum枚举
5annotation:注解interface
6)primitive type:基本数据类型
7void
7.类的加载器加载配置文件
Test
public void test3() throws IOException {//方式一Properties pro new Properties();FileInputStream fis new FileInputStream(jdbc.propertities);pro.load(fis);String name pro.getProperty(name);String age pro.getProperty(age);System.out.println(name:age);}
Test
public void test4() throws IOException {//方式二该方式文件放在src目录下ClassLoader classLoader Reflection2Test.class.getClassLoader();InputStream resourceAsStream classLoader.getResourceAsStream(jdbc2.propertities);Properties pro new Properties();pro.load(resourceAsStream);String name pro.getProperty(name);String age pro.getProperty(age);System.out.println(name:age);}
六反射的实例
1.获取所有的属性方法构造器
Test
public void test2() {Class clazz Person.class;//获取运行时类及其所有父类的public方法Method[] methods clazz.getMethods();for (Method m : methods) {System.out.print(m \t);System.out.println(m.getName());}System.out.println();//获取运行时类所有方法Method[] declaredMethods clazz.getDeclaredMethods();for (Method m : declaredMethods) {System.out.print(m \t);System.out.println(m.getName());}}Test
public void test3() {Class clazz Person.class;//获取运行时类所有public的构造器Constructor[] constructors clazz.getConstructors();for(Constructor c:constructors){System.out.println(c);}System.out.println();//获取运行时类所有构造器Constructor[] constructors2 clazz.getDeclaredConstructors();for(Constructor c:constructors2){System.out.println(c);}}
Test
public void test4() throws Exception {//获取当前运行时类父类的对象Class superclass Person.class.getSuperclass();System.out.println(superclass);
}
2.获取指定的属性方法构造器
Test
public void filedTest() throws Exception {Class clazzPerson.class;Object obj clazz.newInstance();Person per (Person) obj;//获取指定属性Field name clazz.getDeclaredField(name);//给指定对象属性复制name.set(per,tom);System.out.println(name.get(per));//给静态属性赋值并获取Field hobbies clazz.getDeclaredField(hobbies);hobbies.set(clazz,baskball);System.out.println(hobbies.get(clazz));
}Test
public void methodTest() throws Exception {Class clazzPerson.class;Object obj clazz.newInstance();Person per (Person) obj;//获取public 无参方法 并调用Method info clazz.getDeclaredMethod(info);info.invoke(per);//获取public 有参方法 并调用Method getNation clazz.getDeclaredMethod(getNation,String.class);getNation.invoke(per,china);//获取静态方法并调用Method study clazz.getDeclaredMethod(study);study.invoke(clazz);//获取私有方法并调用Method getAge clazz.getDeclaredMethod(getAge, int.class);//设置为可访问getAge.setAccessible(true);System.out.println(getAge.invoke(per, 20));}Test
public void ConstructTest() throws Exception {Class clazz Person.class;//该方法为常用用法具有通用性Object o clazz.newInstance();System.out.println(o);//获取带有参数的构造器不常用Constructor constructor clazz.getDeclaredConstructor(int.class, String.class);constructor.setAccessible(true);Object jerry constructor.newInstance(15, jerry);System.out.println(jerry);}七反射的应用动态代理
1.静态代理
interface ColthFactory{void produceColth();
}
//代理类
class ProxyColthFactory implements ColthFactory{private ColthFactory factory;public ProxyColthFactory(ColthFactory factory){this.factoryfactory;}Overridepublic void produceColth() {System.out.println(生产衣服之前的准备工作);factory.produceColth();System.out.println(生产衣服之后的收尾工作);}
}
//被代理类
class NIkeColthFactory implements ColthFactory{Overridepublic void produceColth() {System.out.println(NIKE生产衣服);}
}public class StaticProxyTest {public static void main(String[] args) {//创建被代理类对象NIkeColthFactory nike new NIkeColthFactory();//创建代理类对象ProxyColthFactory proxy new ProxyColthFactory(nike);//调用方法proxy.produceColth();}
}
2.动态代理
class ProxyFactory{public static Object getInstance(Object obj){MyInvocationHandler hander new MyInvocationHandler();hander.blind(obj);return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), hander);}
}class MyInvocationHandler implements InvocationHandler{private Object obj; //赋值时必须传被代理类实例public void blind(Object obj){this.objobj;}Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object invoke method.invoke(obj, args);// method为代理类调用的方法此方法也作为被代理类对象要调用的方法return invoke;}
}public class ProxyTest {public static void main(String[] args) {NIkeColthFactory nike new NIkeColthFactory();ColthFactory colthFactory (ColthFactory) ProxyFactory.getInstance(nike);colthFactory.produceColth();}
}