⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.htm

📁 这是一个实现登陆的页面程序
💻 HTM
📖 第 1 页 / 共 4 页
字号:
      &nbsp;System.out.println("-----");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;} catch (Throwable e) {<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp;}<BR>&nbsp; &nbsp;}<BR>}<BR><BR>这个程序首先取得 method1 类的描述,然后调用 
      getDeclaredMethods 来获取一系列的 Method 对象,它们分别描述了定义在类中的每一个方法,包括 public 
      方法、protected 方法、package 方法和 private 方法等。如果你在程序中使用 getMethods 来代替 
      getDeclaredMethods,你还能获得继承来的各个方法的信息。<BR><BR>取得了 Method 
      对象列表之后,要显示这些方法的参数类型、异常类型和返回值类型等就不难了。这些类型是基本类型还是类类型,都可以由描述类的对象按顺序给出。<BR><BR>输出的结果如下:<BR><BR>name 
      = f1<BR><BR>decl class = class method1<BR><BR>param #0 class 
      java.lang.Object<BR><BR>param #1 int<BR><BR>exc #0 class 
      java.lang.NullPointerException<BR><BR>return type = 
      int<BR><BR>-----<BR><BR>name = main<BR><BR>decl class = class 
      method1<BR><BR>param #0 class [Ljava.lang.String;<BR><BR>return type = 
      void<BR><BR>-----<BR><BR><BR>4.获取构造器信息<BR><BR>获取类构造器的用法与上述获取方法的用法类似,如:<BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class constructor1 {<BR>&nbsp; 
      &nbsp;public constructor1() {<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; 
      &nbsp;protected constructor1(int i, double d) {<BR>&nbsp; 
      &nbsp;}<BR><BR>&nbsp; &nbsp;public static void main(String args[]) 
      {<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class cls = Class.forName("constructor1");<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Constructor ctorlist[] = 
      cls.getDeclaredConstructors();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;for (int i = 0; i &lt; ctorlist.length; i++) {<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Constructor ct = 
      ctorlist[i];<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("name = " + ct.getName());<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("decl class = 
      " + ct.getDeclaringClass());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class pvec[] = ct.getParameterTypes();<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j = 0; j &lt; 
      pvec.length; j++)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;System.out.println("param #" + j + " " + 
      pvec[j]);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Class 
      evec[] = ct.getExceptionTypes();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;for (int j = 0; j &lt; evec.length; j++)<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("exc #" + j + " " + evec[j]);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("-----");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;} catch (Throwable e) {<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp;}<BR>&nbsp; 
      &nbsp;}<BR>}<BR><BR>这个例子中没能获得返回类型的相关信息,那是因为构造器没有返回类型。<BR><BR>这个程序运行的结果是:<BR><BR>name 
      = constructor1<BR><BR>decl class = class 
      constructor1<BR><BR>-----<BR><BR>name = constructor1<BR><BR>decl class = 
      class constructor1<BR><BR>param #0 int<BR><BR>param #1 
      double<BR><BR>-----<BR><BR>5.获取类的字段(域)<BR><BR>找出一个类中定义了哪些数据字段也是可能的,下面的代码就在干这个事情:<BR><BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class field1 {<BR>&nbsp; &nbsp;private 
      double d;<BR>&nbsp; &nbsp;public static final int i = 37;<BR>&nbsp; 
      &nbsp;String s = "testing";<BR><BR>&nbsp; &nbsp;public static void 
      main(String args[]) {<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Class cls = Class.forName("field1");<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Field fieldlist[] = 
      cls.getDeclaredFields();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for 
      (int i = 0; i &lt; fieldlist.length; i++) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Field fld = fieldlist[i];<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("name = " + 
      fld.getName());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("decl class = " + 
      fld.getDeclaringClass());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;System.out.println("type = " + fld.getType());<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int mod = 
      fld.getModifiers();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("modifiers = " + 
      Modifier.toString(mod));<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;System.out.println("-----");<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;}<BR>&nbsp; &nbsp; &nbsp; &nbsp;} catch (Throwable e) 
      {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
      &nbsp;}<BR>}<BR><BR>这个例子和前面那个例子非常相似。例中使用了一个新东西 Modifier,它也是一个 reflection 
      类,用来描述字段成员的修饰语,如“private int”。这些修饰语自身由整数描述,而且使用 Modifier.toString 
      来返回以“官方”顺序排列的字符串描述 (如“static”在“final”之前)。这个程序的输出是:<BR><BR>name = 
      d<BR><BR>decl class = class field1<BR><BR>type = double<BR><BR>modifiers = 
      private<BR><BR>-----<BR><BR>name = i<BR><BR>decl class = class 
      field1<BR><BR>type = int<BR><BR>modifiers = public static 
      final<BR><BR>-----<BR><BR>name = s<BR><BR>decl class = class 
      field1<BR><BR>type = class java.lang.String<BR><BR>modifiers 
      =<BR><BR>-----<BR><BR>和获取方法的情况一下,获取字段的时候也可以只取得在当前类中申明了的字段信息 
      (getDeclaredFields),或者也可以取得父类中定义的字段 (getFields) 
      。<BR><BR><BR>6.根据方法的名称来执行方法<BR><BR>文本到这里,所举的例子无一例外都与如何获取类的信息有关。我们也可以用 
      reflection 来做一些其它的事情,比如执行一个指定了名称的方法。下面的示例演示了这一操作:<BR><BR>import 
      java.lang.reflect.*;<BR>public class method2 {<BR>&nbsp; &nbsp;public int 
      add(int a, int b) {<BR>&nbsp; &nbsp; &nbsp; &nbsp;return a + b;<BR>&nbsp; 
      &nbsp;}<BR>&nbsp; &nbsp;public static void main(String args[]) {<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;Class cls = Class.forName("method2");<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class partypes[] = new Class[2];<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;partypes[0] = Integer.TYPE;<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;partypes[1] = Integer.TYPE;<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;Method meth = cls.getMethod("add", 
      partypes);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method2 methobj = 
      new method2();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object 
      arglist[] = new Object[2];<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;arglist[0] = new Integer(37);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;arglist[1] = new Integer(47);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;Object retobj = meth.invoke(methobj, arglist);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Integer retval = (Integer) retobj;<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println(retval.intValue());<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;} catch (Throwable e) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
      &nbsp;}<BR>}<BR><BR>假如一个程序在执行的某处的时候才知道需要执行某个方法,这个方法的名称是在程序的运行过程中指定的 
      (例如,JavaBean 开发环境中就会做这样的事),那么上面的程序演示了如何做到。<BR><BR>上例中,getMethod 
      用于查找一个具有两个整型参数且名为 add 的方法。找到该方法并创建了相应的 Method 
      对象之后,在正确的对象实例中执行它。执行该方法的时候,需要提供一个参数列表,这在上例中是分别包装了整数 37 和 47 的两个 Integer 
      对象。执行方法的返回的同样是一个 Integer 对象,它封装了返回值 
      84。<BR><BR>7.创建新的对象<BR><BR>对于构造器,则不能像执行方法那样进行,因为执行一个构造器就意味着创建了一个新的对象 
      (准确的说,创建一个对象的过程包括分配内存和构造对象)。所以,与上例最相似的例子如下:<BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class constructor2 {<BR>&nbsp; 
      &nbsp;public constructor2() {<BR>&nbsp; &nbsp;}<BR><BR>&nbsp; &nbsp;public 
      constructor2(int a, int b) {<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("a = " + a + " b = " + b);<BR>&nbsp; 
      &nbsp;}<BR><BR>&nbsp; &nbsp;public static void main(String args[]) 
      {<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class cls = Class.forName("constructor2");<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Class partypes[] = new Class[2];<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;partypes[0] = Integer.TYPE;<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;partypes[1] = Integer.TYPE;<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Constructor ct = 
      cls.getConstructor(partypes);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;Object arglist[] = new Object[2];<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;arglist[0] = new Integer(37);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;arglist[1] = new Integer(47);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Object retobj = ct.newInstance(arglist);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp;} catch (Throwable e) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
      &nbsp;}<BR>}<BR><BR>根据指定的参数类型找到相应的构造函数并执行它,以创建一个新的对象实例。使用这种方法可以在程序运行时动态地创建对象,而不是在编译的时候创建对象,这一点非常有价值。<BR><BR>8.改变字段(域)的值<BR><BR>reflection 
      的还有一个用处就是改变对象数据字段的值。reflection 
      可以从正在运行的程序中根据名称找到对象的字段并改变它,下面的例子可以说明这一点:<BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class field2 {<BR>&nbsp; &nbsp;public 
      double d;<BR><BR>&nbsp; &nbsp;public static void main(String args[]) 
      {<BR>&nbsp; &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class cls = Class.forName("field2");<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;Field fld = cls.getField("d");<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;field2 f2obj = new field2();<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;System.out.println("d = " + f2obj.d);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;fld.setDouble(f2obj, 12.34);<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("d = " + f2obj.d);<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp;} catch (Throwable e) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;}<BR>&nbsp; &nbsp;}<BR>}<BR><BR>这个例子中,字段 d 的值被变为了 
      12.34。<BR><BR>9.使用数组<BR><BR>本文介绍的 reflection 的最后一种用法是创建的操作数组。数组在 Java 
      语言中是一种特殊的类类型,一个数组的引用可以赋给 Object 引用。观察下面的例子看看数组是怎么工作的:<BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class array1 {<BR>&nbsp; &nbsp;public 
      static void main(String args[]) {<BR>&nbsp; &nbsp; &nbsp; &nbsp;try 
      {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Class cls = 
      Class.forName("java.lang.String");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;Object arr = Array.newInstance(cls, 10);<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;Array.set(arr, 5, "this is a test");<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;String s = (String) Array.get(arr, 
      5);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println(s);<BR>&nbsp; &nbsp; &nbsp; &nbsp;} catch 
      (Throwable e) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.err.println(e);<BR>&nbsp; &nbsp; &nbsp; &nbsp;}<BR>&nbsp; 
      &nbsp;}<BR>}<BR><BR>例中创建了 10 个单位长度的 String 数组,为第 5 
      个位置的字符串赋了值,最后将这个字符串从数组中取得并打印了出来。<BR><BR>下面这段代码提供了一个更复杂的例子:<BR><BR>import 
      java.lang.reflect.*;<BR><BR>public class array2 {<BR>&nbsp; &nbsp;public 
      static void main(String args[]) {<BR>&nbsp; &nbsp; &nbsp; &nbsp;int dims[] 
      = new int[]{5, 10, 15};<BR>&nbsp; &nbsp; &nbsp; &nbsp;Object arr = 
      Array.newInstance(Integer.TYPE, dims);<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;Object arrobj = Array.get(arr, 3);<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;Class cls = arrobj.getClass().getComponentType();<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp;System.out.println(cls);<BR>&nbsp; &nbsp; &nbsp; &nbsp;arrobj 
      = Array.get(arrobj, 5);<BR>&nbsp; &nbsp; &nbsp; &nbsp;Array.setInt(arrobj, 
      10, 37);<BR>&nbsp; &nbsp; &nbsp; &nbsp;int arrcast[][][] = (int[][][]) 
      arr;<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println(arrcast[3][5][10]);<BR>&nbsp; 
      &nbsp;}<BR>}<BR>例中创建了一个 5 x 10 x 15 的整型数组,并为处于 [3][5][10] 的元素赋了值为 
      37。注意,数组实际上就是数组的数组,例如,第一个 Array.get 之后,arrobj 是一个 10 x 15 
      的数组。进而取得其中的一个元素,即长度为 15 的数组,并使用 Array.setInt 为它的第 10 
      个元素赋值。<BR><BR>注意创建数组时的类型是动态的,在编译时并不知道其类型 </DIV>
      <P class=postfoot>posted on 2006-09-08 17:56 <A 
      href="http://www.blogjava.net/liulu/">刘璐</A> 阅读(60) <A 
      href="http://www.blogjava.net/liulu/archive/2006/09/08/68567.aspx#Post">评论(0)</A> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -