当前位置: 首页 > news >正文

网站建设找工作wordpress页面怎么添加

网站建设找工作,wordpress页面怎么添加,深圳南山区住房和建设局网站官网,门网站源码一、概念 主要是指程序可以访问#xff0c;检测和修改它本身状态或行为的一种能力#xff0c;并能根据自身行为的状态和结果#xff0c;调整或修改应用所描述行为的状态和相关的语义。在java中#xff0c;只要给定类的名字#xff0c; 那么就可以通过反射机制来获得类的所…一、概念 主要是指程序可以访问检测和修改它本身状态或行为的一种能力并能根据自身行为的状态和结果调整或修改应用所描述行为的状态和相关的语义。在java中只要给定类的名字 那么就可以通过反射机制来获得类的所有信息。 反射是Java中一种强大的工具能够使我们很方便的创建灵活的代码这些代码可以再运行时装配无需在组件之间进行源代码链接。但是反射使用不当会成本很高 类中有什么信息利用反射机制就能可以获得什么信息不过前提是得知道类的名字。 二、作用 1在运行时判断任意一个对象所属的类 2在运行时获取类的对象 3在运行时访问java对象的属性方法构造方法等。 三、优点与缺点 首先要搞清楚为什么要用反射机制直接创建对象不就可以了吗这就涉及到了动态与静态的概念。  静态编译在编译时确定类型绑定对象即通过。  动态编译运行时确定类型绑定对象。动态编译最大限度发挥了java的灵活性体现了多态的应用有以降低类之间的藕合性。 反射机制的优点可以实现动态创建对象和编译体现出很大的灵活性特别是在J2EE的开发中它的灵活性就表现的十分明显。通过反射机制我们可以获得类的各种内容进行了反编译。对于JAVA这种先编译再运行的语言来说反射机制可以使代码更加灵活更加容易实现面向对象。 比如一个大型的软件不可能一次就把把它设计的很完美当这个程序编译后发布了当发现需要更新某些功能时我们不可能要用户把以前的卸载再重新安装新的版本假如这样的话这个软件肯定是没有多少人用的。采用静态的话需要把整个程序重新编译一次才可以实现功能的更新而采用反射机制的话它就可以不用卸载只需要在运行时才动态的创建和编译就可以实现该功能。  反射机制的缺点对性能有影响。使用反射基本上是一种解释操作我们可以告诉JVM我们希望做什么并且它 满足我们的要求。这类操作总是慢于只直接执行相同的操作。 四、示例 1、通过一个对象获取完整的包名和类名 注所有类的对象其实都是Class类的实例 package Reflect;class Demo{//other codes... }class hello{public static void main(String[] args) {Demo demonew Demo();System.out.println(demo.getClass().getName());} } 运行结果Reflect.Demo 2、实例化Class类对象package Reflect;class Demo{//other codes... }class hello{public static void main(String[] args) {Class? demo1null;Class? demo2null;Class? demo3null;try{//一般尽量采用这种形式demo1Class.forName(Reflect.Demo);}catch(Exception e){e.printStackTrace();}demo2new Demo().getClass();demo3Demo.class;System.out.println(类名称 demo1.getName());System.out.println(类名称 demo2.getName());System.out.println(类名称 demo3.getName());} } 运行结果类名称 Reflect.Demo 类名称 Reflect.Demo 类名称 Reflect.Demo 3、通过Class实例化其他类的对象package Reflect;class Person{public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}Overridepublic String toString(){return [this.name this.age];}private String name;private int age; }class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}Person pernull;try {per(Person)demo.newInstance();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();}per.setName(Rollen);per.setAge(20);System.out.println(per);} } 运行结果[Rollen 20]但是注意一下当我们把Person中的默认的无参构造函数取消的时候比如自己定义只定义一个有参数的构造函数之后会出现错误 比如定义了一个构造函数 public Person(String name, int age) { this.ageage;this.namename; }然后继续运行上面的程序会出现java.lang.InstantiationException: Reflect.Person at java.lang.Class.newInstance0(Class.java:340) at java.lang.Class.newInstance(Class.java:308) at Reflect.hello.main(hello.java:39) Exception in thread main java.lang.NullPointerException at Reflect.hello.main(hello.java:47)       所以大家以后再编写使用Class实例化其他类的对象的时候一定要自己定义无参的构造函数。4、通过Class调用其他类中的构造函数 也可以通过这种方式通过Class创建其他类的对象 package Reflect;import java.lang.reflect.Constructor;class Person{public Person() { }public Person(String name){this.namename;}public Person(int age){this.ageage;}public Person(String name, int age) {this.ageage;this.namename;}public String getName() {return name;}public int getAge() {return age;}Overridepublic String toString(){return [this.name this.age];}private String name;private int age; }class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}Person per1null;Person per2null;Person per3null;Person per4null;//取得全部的构造函数Constructor? cons[]demo.getConstructors();try{per1(Person)cons[0].newInstance();per2(Person)cons[1].newInstance(Rollen);per3(Person)cons[2].newInstance(20);per4(Person)cons[3].newInstance(Rollen,20);}catch(Exception e){e.printStackTrace();}System.out.println(per1);System.out.println(per2);System.out.println(per3);System.out.println(per4);} } 运行结果 [null 0] [Rollen 0] [null 20] [Rollen 20] 5、返回一个类实现的接口package Reflect;interface China{public static final String nameRollen;public static int age20;public void sayChina();public void sayHello(String name, int age); }class Person implements China{public Person() {}public Person(String sex){this.sexsex;}public String getSex() {return sex;}public void setSex(String sex) {this.sex sex;}Overridepublic void sayChina(){System.out.println(hello ,china);}Overridepublic void sayHello(String name, int age){System.out.println(name age);}private String sex; }class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}//保存所有的接口Class? intes[]demo.getInterfaces();for (int i 0; i intes.length; i) {System.out.println(实现的接口 intes[i].getName());}} } 运行结果实现的接口 Reflect.China以下几个例子都会用到这个例子的Person类所以为节省篇幅此处不再粘贴Person的代码部分只粘贴主类hello的代码6、取得其他类中的父类class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}//取得父类Class? tempdemo.getSuperclass();System.out.println(继承的父类为 temp.getName());} } 运行结果继承的父类为 java.lang.Object 7、获得其他类中的全部构造函数//这个例子需要在程序开头添加import java.lang.reflect.*; class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}Constructor?cons[]demo.getConstructors();for (int i 0; i cons.length; i) {System.out.println(构造方法 cons[i]);}} } 运行结果构造方法 public Reflect.Person() 构造方法 public Reflect.Person(java.lang.String) class hello{public static void main(String[] args) {Class? demonull;try{demoClass.forName(Reflect.Person);}catch (Exception e) {e.printStackTrace();}Constructor?cons[]demo.getConstructors();for (int i 0; i cons.length; i) {Class? p[]cons[i].getParameterTypes();System.out.print(构造方法 );int mocons[i].getModifiers();System.out.print(Modifier.toString(mo) );System.out.print(cons[i].getName());System.out.print(();for(int j0;jp.length;j){System.out.print(p[j].getName() argi);if(jp.length-1){System.out.print(,);}}System.out.println(){});}} } 运行结果构造方法 public Reflect.Person(){} 构造方法 public Reflect.Person(java.lang.String arg1){}8、取得其他类的全部属性将这些整理在一起也就是通过class取得一个类的全部框架 class hello {public static void main(String[] args) {Class? demo null;try {demo Class.forName(Reflect.Person);} catch (Exception e) {e.printStackTrace();}System.out.println(本类属性);// 取得本类的全部属性Field[] field demo.getDeclaredFields();for (int i 0; i field.length; i) {// 权限修饰符int mo field[i].getModifiers();String priv Modifier.toString(mo);// 属性类型Class? type field[i].getType();System.out.println(priv type.getName() field[i].getName() ;);}System.out.println(实现的接口或者父类的属性);// 取得实现的接口或者父类的属性Field[] filed1 demo.getFields();for (int j 0; j filed1.length; j) {// 权限修饰符int mo filed1[j].getModifiers();String priv Modifier.toString(mo);// 属性类型Class? type filed1[j].getType();System.out.println(priv type.getName() filed1[j].getName() ;);}} } 运行结果 本类属性 private java.lang.String sex; 实现的接口或者父类的属性 public static final java.lang.String name; public static final int age; 9、通过反射调用其他类中的方法class hello {public static void main(String[] args) {Class? demo null;try {demo Class.forName(Reflect.Person);} catch (Exception e) {e.printStackTrace();}try{//调用Person类中的sayChina方法Method methoddemo.getMethod(sayChina);method.invoke(demo.newInstance());//调用Person的sayHello方法methoddemo.getMethod(sayHello, String.class,int.class);method.invoke(demo.newInstance(),Rollen,20);}catch (Exception e) {e.printStackTrace();}} } 运行结果hello ,china Rollen 2010、调用其他类的set和get方法 class hello {public static void main(String[] args) {Class? demo null;Object objnull;try {demo Class.forName(Reflect.Person);} catch (Exception e) {e.printStackTrace();}try{objdemo.newInstance();}catch (Exception e) {e.printStackTrace();}setter(obj,Sex,男,String.class);getter(obj,Sex);}/*** param obj 操作的对象* param att 操作的属性* */public static void getter(Object obj, String att) {try {Method method obj.getClass().getMethod(get att);System.out.println(method.invoke(obj));} catch (Exception e) {e.printStackTrace();}}/*** param obj 操作的对象 * param att 操作的属性* param value 设置的值* param type 参数的属性* */public static void setter(Object obj, String att, Object value,Class? type) {try {Method method obj.getClass().getMethod(set att, type);method.invoke(obj, value);} catch (Exception e) {e.printStackTrace();}} }// end class 运行结果 男 11、通过反射操作属性 class hello {public static void main(String[] args) throws Exception {Class? demo null;Object obj null;demo Class.forName(Reflect.Person);obj demo.newInstance();Field field demo.getDeclaredField(sex);field.setAccessible(true);field.set(obj, 男);System.out.println(field.get(obj));} }// end class 12、通过反射取得并修改数组的信息import java.lang.reflect.*;class hello{public static void main(String[] args) {int[] temp{1,2,3,4,5};Class?demotemp.getClass().getComponentType();System.out.println(数组类型 demo.getName());System.out.println(数组长度 Array.getLength(temp));System.out.println(数组的第一个元素: Array.get(temp, 0));Array.set(temp, 0, 100);System.out.println(修改之后数组第一个元素为 Array.get(temp, 0));} } 运行结果数组类型 int 数组长度 5 数组的第一个元素: 1 修改之后数组第一个元素为 100 13、通过反射修改数组大小class hello{public static void main(String[] args) {int[] temp{1,2,3,4,5,6,7,8,9};int[] newTemp(int[])arrayInc(temp,15);print(newTemp);System.out.println();String[] atr{a,b,c};String[] str1(String[])arrayInc(atr,8);print(str1);}/*** 修改数组大小* */public static Object arrayInc(Object obj,int len){Class?arrobj.getClass().getComponentType();Object newArrArray.newInstance(arr, len);int coArray.getLength(obj);System.arraycopy(obj, 0, newArr, 0, co);return newArr;}/*** 打印* */public static void print(Object obj){Class?cobj.getClass();if(!c.isArray()){return;}System.out.println(数组长度为 Array.getLength(obj));for (int i 0; i Array.getLength(obj); i) {System.out.print(Array.get(obj, i) );}} } 运行结果数组长度为 15 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 数组长度为 8 a b c null null null null null 14、动态代理首先来看看如何获得类加载器class test{} class hello{public static void main(String[] args) {test tnew test();System.out.println(类加载器 t.getClass().getClassLoader().getClass().getName());} } 运行结果类加载器 sun.misc.Launcher$AppClassLoader其实在java中有三种类类加载器 1Bootstrap ClassLoader 此加载器采用c编写一般开发中很少见。 2Extension ClassLoader 用来进行扩展类的加载一般对应的是jre\lib\ext目录中的类 3AppClassLoader 加载classpath指定的类是最常用的加载器。同时也是java中默认的加载器。 如果想要完成动态代理首先需要定义一个InvocationHandler接口的子类已完成代理的具体操作。 package Reflect;import java.lang.reflect.*;//定义项目接口 interface Subject {public String say(String name, int age); }// 定义真实项目 class RealSubject implements Subject {Overridepublic String say(String name, int age) {return name age;} }class MyInvocationHandler implements InvocationHandler {private Object obj null;public Object bind(Object obj) {this.obj obj;return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);}Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {Object temp method.invoke(this.obj, args);return temp;} }class hello {public static void main(String[] args) {MyInvocationHandler demo new MyInvocationHandler();Subject sub (Subject) demo.bind(new RealSubject());String info sub.say(Rollen, 20);System.out.println(info);} } 运行结果 Rollen 20类的生命周期 在一个类编译完成之后下一步就需要开始使用类如果要使用一个类肯定离不开JVM。在程序执行中JVM通过装载、链接、初始化这3个步骤完成。 类的装载是通过类加载器完成的加载器将.class文件的二进制文件装入JVM的方法区并且在堆区创建描述这个类的java.lang.Class对象。用来封装数据。 但是同一个类只会被类装载器装载一次。 链接就是把二进制数据组装为可以运行的状态。 链接分为校验准备解析这3个阶段 校验一般用来确认此二进制文件是否适合当前的JVM版本 准备就是为静态成员分配内存空间。并设置默认值 解析指的是转换常量池中的代码作为直接引用的过程直到所有的符号引用都可以被运行程序使用建立完整的对应关系。 完成之后类型也就完成了初始化初始化之后类的对象就可以正常使用了直到一个对象不再使用之后将被垃圾回收。释放空间。当没有任何引用指向Class对象时就会被卸载结束类的生命周期。 五、IoC原理Spring中的IoC的实现原理就是工厂模式加反射机制。 1、我们首先看一下不用反射机制时的工厂模式 /*** 工厂模式*/ interface fruit{public abstract void eat(); }class Apple implements fruit{public void eat(){System.out.println(Apple);} }class Orange implements fruit{public void eat(){System.out.println(Orange);} } // 构造工厂类 // 也就是说以后如果我们在添加其他的实例的时候只需要修改工厂类就行了 class Factory{public static fruit getInstance(String fruitName){fruit fnull;if(Apple.equals(fruitName)){fnew Apple();}if(Orange.equals(fruitName)){fnew Orange();}return f;} }class hello{public static void main(String[] a){fruit fFactory.getInstance(Orange);f.eat();} }当我们在添加一个子类的时候就需要修改工厂类了。如果我们添加太多的子类的时候改的就会很多。2、利用反射机制的工厂模式package Reflect;interface fruit{public abstract void eat(); }class Apple implements fruit{public void eat(){System.out.println(Apple);} }class Orange implements fruit{public void eat(){System.out.println(Orange);} }class Factory{public static fruit getInstance(String ClassName){fruit fnull;try{f(fruit)Class.forName(ClassName).newInstance();}catch (Exception e) {e.printStackTrace();}return f;} }class hello{public static void main(String[] a){fruit fFactory.getInstance(Reflect.Apple);if(f!null){f.eat();}} }现在就算我们添加任意多个子类的时候工厂类就不需要修改。 使用反射机制的工厂模式可以通过反射取得接口的实例但是需要传入完整的包和类名。而且用户也无法知道一个接口有多少个可以使用的子类所以我们通过属性文件的形式配置所需要的子类。 3、使用反射机制并结合属性文件的工厂模式即IoC首先创建一个fruit.properties的资源文件appleReflect.Apple orangeReflect.Orange然后编写主类代码package Reflect;import java.io.*; import java.util.*;interface fruit{public abstract void eat(); }class Apple implements fruit{public void eat(){System.out.println(Apple);} }class Orange implements fruit{public void eat(){System.out.println(Orange);} } //操作属性文件类 class init{public static Properties getPro() throws FileNotFoundException, IOException{Properties pronew Properties();File fnew File(fruit.properties);if(f.exists()){pro.load(new FileInputStream(f));}else{pro.setProperty(apple, Reflect.Apple);pro.setProperty(orange, Reflect.Orange);pro.store(new FileOutputStream(f), FRUIT CLASS);}return pro;} }class Factory{public static fruit getInstance(String ClassName){fruit fnull;try{f(fruit)Class.forName(ClassName).newInstance();}catch (Exception e) {e.printStackTrace();}return f;} }class hello{public static void main(String[] a) throws FileNotFoundException, IOException{Properties proinit.getPro();fruit fFactory.getInstance(pro.getProperty(apple));if(f!null){f.eat();}} } 运行结果 Apple
http://www.yutouwan.com/news/420534/

相关文章:

  • 淮安网站设计公司wordpress文章签名插件
  • 毕设做网站答辩一般问什么深圳官方宣布解封时间
  • 能播放优酷视频的网站怎样做wordpress多用户博客系统
  • 网站点击率原因网络推广最好的网站有哪些
  • 泰安建设企业网站生活服务网站建设
  • 免费网站发布怎么做的百度搜索 网站图片
  • 美容院网站建设金石项目管理软件
  • 南昌网站建设培训班广东seo推广贵不贵
  • wordpress对网站排名网络营销方式有哪些类型
  • 东莞网站优化排名网站卡地亚手表官方网站查询
  • 重庆公司网站酒店网站建设栏目分析
  • 扬州市城乡建设网站怎么用织梦做购物网站
  • 网站建设的一些问题阿里云能做网站么
  • 万能网站网址下载app制作费用是多少
  • 为什么网站找不到了环保设备东莞网站建设
  • 移动建站模板wordpress获取特定分类文章数
  • 企业网站软件下载网站公司哪家好
  • 织梦网站主页代码在后台怎么改周杰伦做的广告网站
  • 南京公司网站建设简单html网页制作代码
  • 专门做试卷的网站建筑工程网cnas
  • 建设金融网站哪家好威海优化公司立找2火星
  • 仿冒网站制作小白网页制作软件
  • 数据处理网站开发天河建设网站制作
  • 个人网站模板 免费WordPress未设置密码用户
  • 惠州惠城区建设网站物流网络化
  • 北京怎样建网站汕头制作企业网站
  • 百度网站链接提交页面外贸网站如何做推广苏州
  • 联雅网站建设公司殡葬类网站建设
  • 网站备案全国合作拍照点什么是营销型网站
  • 建设的招标网站购物商城网站开发公司