关闭

Java反射机制初探

发表于:2012-4-06 09:51

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:octobershiner    来源:51Testing软件测试网采编

  最近和一位师兄交流了一下Java,真可谓是大有收获,让我好好的学习了一下javad的反射机制,同终于明白了spring等框架的一个基本实现的思想,那么今天就和大家分享一下java的反射机制。

  反射,reflection,听其名就像照镜子一样,可以看见自己也可以看见别人的每一部分。在java语言中这是一个很重要的特性。下面是来自sun公司官网关于反射的介绍:

  Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

  The ability to examine and manipulate a Java class from within itself may not sound like very much, but in other programming languages this feature simply doesn't exist. For example, there is no way in a Pascal, C, or C++ program to obtain information about the functions defined within that program.

  One tangible use of reflection is in JavaBeans, where software components can be manipulated visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically loaded.

  那么解释一下就是,反射是java语言的一个特性,它允程序在运行时(注意不是编译的时候)来进行自我检查并且对内部的成员进行操作。例如它允许一个java的类获取他所有的成员变量和方法并且显示出来。这个能特定我们不常看到,但是在其他的比如C或者C++语言中很不就存在这个特性。一个常见的例子是在JavaBean中,一些组件可以通过一个构造器来操作。这个构造器就是用的反射在动态加载的时候来获取的java中类的属性的。

  反射的前传:类类型 Class Class

  java中有一个类很特殊,就是Class类,很多朋友在写程序的时候有用过比如Apple.class来查看类型信息,大家就可以把它理解为封装了类的信息,很多解释说Class类没有构造器,其实是有的,只不过它的构造方法是private的(构造函数还有private的??有,这样是为了禁止开发者去自己创建Class类的实例)。我们可以看一下JDK中源码:

  注释很明确的告诉了我们,这个类是有JVM来创建的,所以我们就不用麻烦了。如果我们拿到一个类的类型信息,就可以利用反射获取其各种成员以及方法了。(注:Class 从JDK1.5版本后就开始更多为泛型服务了)那么我们怎么拿到一个类型的信息呢?假设我们有一个Role类:

  1. package yui;  
  2.    
  3.  /** 
  4.   * A base class having some attributes and methods 
  5.   * @author Octobershiner 
  6.   * @since 2012 3 17 
  7.   *  
  8.   * */ 
  9.  public class Role {  
  10.        
  11.      private String name;  
  12.      private String type;  
  13.        
  14.      // Constructors 
  15.      public Role(){  
  16.          System.out.println("Constructor Role() is invoking");  
  17.      }  
  18.      //私有构造器 
  19.      private Role(String name){  
  20.          this.name = name;  
  21.          System.out.println("Constructor Role(String name) is invoking.");  
  22.      }  
  23.        
  24.      //get and set method 
  25.        
  26.      public String getName() {  
  27.          return name;  
  28.      }  
  29.      public void setName(String name) {  
  30.          this.name = name;  
  31.      }  
  32.      public String getType() {  
  33.          return type;  
  34.      }  
  35.      public void setType(String type) {  
  36.          this.type = type;  
  37.      }  
  38.        
  39.      //override the toString method to show the class 
  40.      @Override 
  41.      public String toString(){  
  42.          return "This is a role called "+this.name;  
  43.      }  
  44.        
  45.  }

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号