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

网站建设费会计域名购买万网

网站建设费会计,域名购买万网,网站展示怎么做,好的seo公司115.反射 反射机制 1.根据配置文件re.properties指定信息,创建Cat对象并调用hi方法 SuppressWarnings({"all"}) public class ReflectionQuestion {public static void main(String[] args) throws IOException {//根据配置文件 re.properties 指定信息…

115.反射

反射机制

1.根据配置文件re.properties指定信息,创建Cat对象并调用hi方法

@SuppressWarnings({"all"})
public class ReflectionQuestion {public static void main(String[] args) throws IOException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\re.properties"));String classfullpath = properties.get("classfullpath").toString();Object method = properties.get("method").toString();System.out.println("classfullpath=" + classfullpath);System.out.println("method=" + method);
​//2.创建对象,传统的方法行不通 -> 反射机制//new classfullpath();}
}

2.这样的需求在学习框架的时候特别多,即通过外部文件配置,在不修改源代码的情况下,来控制程序,也符合设计模式的ocp原则(开闭原则)

反射快速入门
public class ReflectionQuestion {public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\re.properties"));String classfullpath = properties.get("classfullpath").toString();String methodName = properties.get("method").toString();System.out.println("classfullpath=" + classfullpath);System.out.println("method=" + methodName);
​//2.创建对象,传统的方法行不通 -》 反射机制//new classfullpath();
​//3.使用反射机制//(1)加载类,返回Class类型的对象Class cls = Class.forName(classfullpath);//(2) 通过cls 得到你加载的类 com.reflection.question.Cat 的对象实例Object o = cls.newInstance();System.out.println("o的运行类型=" + o.getClass()); //运行类型//(3) 通过 cls 得到你加载的类 com.reflection.question.Cat 的 mothName 的方法对象//    即:在反射中,可以把方法视作对象(万物皆对象)Method method1 = cls.getMethod(methodName);//(4) 通过method1 调用方法:即通过方法对象来实现调用System.out.println("=================");method1.invoke(o);//传统方法 对象.方法() , 反射机制 方法.invok(对象)}
}
反射原理图
  1. 反射机制允许程序在执行期借助于ReflectionAPI取得任何类的内部信息(比如成员变量,构造器,成员方法等等),并能操作对象的属性及方法。反射在设计模式和框架底层都会用到

  2. 加载完类之后,在堆中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以称之为反射

反射相关类

Java反射机制可以完成

  1. 在运行时判断任意一个对象所属的类

  2. 在运行时构造任意一个类的对象

  3. 在运行时得到任意一个类所具有的成员变量和方法

  4. 在运行时调用任意一个对象的成员变量和方法

  5. 生成动态代理

反射相关的主要类

  1. java.lang.Class:代表一个类,Class对象标识某个类加载后在堆中的对象

  2. java.lang..reflect.Method:代表类的方法,Method对象表示某个类的方法

  3. java.lang..reflect.Field:代表类的成员变量,Field对象标识某个类的成员变量

    //得到name字段
    //getField不能得到私有的属性 
    Field name = cls.getField("name");
    System.out.println(nameField.get(o));//传统写法 对象.成员变量 , 反射:成员变量对象.get(对象)  
  4. java.lang..reflect.Constructor:代表类的构造方法,Constructor表示一个构造器

    Constructor constructor = cks.getConstructor();//()中可以指定构造器参数类型,返回无参构造器
    System.out.println(constructor);//Cat()
    ​
    Constructor constructor2 = cls.getConstructor(String.class);//这里传入的String.class就是String类型的Class对象
    System.out.println(constructor2);//Cat(String name)
反射调用优化

反射优点和缺点

优点:可以动态的创建和使用对象(也是框架底层核心),使用灵活,没有反射机制,框架技术就数去底层支撑

缺点:使用反射基本是解释执行,对执行速度有影响

public class ReflectionQuestion {public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {m1();m2();}//传统方法调用hi
​public static void m1(){long start = System.currentTimeMillis();Cat cat = new Cat();for (int i = 0;i < 90000000;i++){cat.hi();}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//3}
​//反射机制调用方法hipublic static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class<?> cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//286}
}

优化-关闭访问检查

  1. Method和Field、Constructor对象都有setAccessible()方法

  2. setAccessible作用是启动和禁用访问安全检查的开关

  3. 参数值为true表示 反射的对象在使用时取消访问检查,提高反射的效率,反射值为false则表示反射的对象执行访问检查

//反射优化public static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");hi.setAccessible(true);//优化long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("m3方法来调用hi的时间 耗时" + (end - start));//115}
}
http://www.yayakq.cn/news/750011/

相关文章:

  • 为什么我自己做的网站搜索不到宁波seo搜索平台推广专业
  • 公司网站设计定制php综合网站源码
  • wordpress搭电影网站洛阳建站
  • 三亚市住房和城乡建设厅网站广州网站二级等保
  • 做网站用采集wordpress导航栏怎么设置
  • 什么网站百度的收录高青山湖网站建设
  • 杭州微信网站制作外贸购物网站模板
  • google下载安卓版seo关键词优化策略
  • 做国外网站什么好seo排名快速上升
  • 做外贸网站空间多少g页面设计的软件
  • seo外链网站荆楚网微信公众平台下载
  • 网站收索流量网站建设珠江摩尔
  • 步步高学习机进网站怎么做seo应用领域
  • 建设集团有限公司网站拼多多网站建设过程
  • 程序员给女盆友做的网站域名注册信息可以在哪里找到
  • 网站空间与域名的关系自家企业网络推广
  • 安徽省建设厅网站工程师查询临沂网站建设中企动力
  • wordpress系统安装网站优化一般要怎么做
  • 局域网网站开发网站没有备案时
  • 建站推广外包常州微网站建设
  • 郴州网站建设公司哪里有企业名录查询
  • 怎么做网站广告位惠州网红
  • 网站关键词的布局用liferay做的网站
  • 只做网站的2008iis7怎么搭建网站
  • 游戏开发公司哪家好优化网站页面
  • 如何网站增加域名东莞网络推广平台
  • 免费建立自己微网站吗包装设计效果图生成器
  • 网站做SEO优化多少钱河南seo网站多少钱
  • 丹阳网站建设如何免费搭建贴吧系统网站
  • 广告网站设计方案太原seo网站管理