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

📄 a.htm

📁 这是一个实现登陆的页面程序
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://www.blogjava.net/liulu/archive/2006/09/08/68567.aspx -->
<HTML><HEAD id=Head><TITLE>java反射机制 - 小曰的Java博客 - BlogJava</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META http-equiv=Expires content="Mon, 09 Oct 2006 01:02:15 GMT">
<META content="博客 blog java blogjava" name=keywords><LINK id=MainCss 
href="a.files/style.css" type=text/css rel=stylesheet><LINK id=RSSLink title=RSS 
href="http://www.blogjava.net/liulu/rss.aspx" type=application/rss+xml 
rel=alternate>
<META content="MSHTML 6.00.2800.1492" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 onsubmit="javascript:return WebForm_OnSubmit();" 
action=68567.aspx method=post>
<DIV><INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT 
id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT 
id="&#13;&#10;__VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV>
<SCRIPT type=text/javascript>
<!--
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</SCRIPT>

<SCRIPT src="a.files/WebResource.axd" type=text/javascript></SCRIPT>

<SCRIPT language=JavaScript>
									function ctlent(evt)
											{
												if(evt.ctrlKey && evt.keyCode == 13)
												{	
													try
													{
														TempSave('AnonymousPostComment1_tbComment');
													}
													catch(ex)
													{
													}
													finally
													{
													    __doPostBack('AnonymousPostComment1$btnSubmit','')
													}
												}
		
												}</SCRIPT>

<SCRIPT language=JavaScript>function SetReplyAuhor(author){document.getElementById('AnonymousPostComment1_tbComment').value+="@"+author+"\n";document.getElementById('AnonymousPostComment1_tbComment').focus();return false}</SCRIPT>

<SCRIPT src="D:\20061009\a.files\WebResource(1).axd" 
type=text/javascript></SCRIPT>

<SCRIPT type=text/javascript>
<!--
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
// -->
</SCRIPT>

<TABLE class=Framework cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD colSpan=2>
      <DIV id=top>
      <TABLE cellSpacing=0 cellPadding=8 width="100%">
        <TBODY>
        <TR>
          <TD>
            <H1><A class=headermaintitle id=Header1_HeaderTitle 
            href="http://www.blogjava.net/liulu/">小曰的Java博客</A></H1>Open &amp; 
            Open </TD></TR></TBODY></TABLE></DIV>
      <DIV id=sub>
      <DIV class=BlogStats>随笔 - 12, 文章 - 2, 评论 - 0, 引用 - 
  0</DIV></DIV></TD></TR></TBODY></TABLE>
<TABLE class=Framework cellSpacing=0 cellPadding=0 width="100%">
  <TBODY>
  <TR>
    <TD class=LeftCell rowSpan=2>
      <DIV id=leftmenu>数据加载中…… </DIV></TD>
    <TD class=MainCell>
      <DIV id=main>
      <DIV class=post>
      <H2><A id=viewpost1_TitleUrl 
      href="http://www.blogjava.net/liulu/archive/2006/09/08/68567.html">java反射机制</A> 
      </H2>
      <DIV class=postbody>Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 
      程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性。例如,使用它能获得 Java 类中各成员的名称并显示出来。<BR><BR>Java 
      的这一能力在实际应用中也许用得不是很多,但是在其它的程序设计语言中根本就不存在这一特性。例如,Pascal、C 或者 C++ 
      中就没有办法在程序中获得函数定义相关的信息。<BR><BR>JavaBean 是 reflection 
      的实际应用之一,它能让一些工具可视化的操作软件组件。这些工具通过 reflection 动态的载入并取得 Java 组件(类) 
      的属性。<BR><BR><BR><BR>1. 一个简单的例子<BR><BR>考虑下面这个简单的例子,让我们看看 reflection 
      是如何工作的。<BR><BR>import java.lang.reflect.*;<BR>public class DumpMethods 
      {<BR>&nbsp; &nbsp;public static void main(String args[]) {<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp;try {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;Class c = Class.forName(args[0]);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Method m[] = c.getDeclaredMethods();<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;for (int i = 0; i &lt; m.length; i++)<BR>&nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println(m[i].toString());<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>java DumpMethods 
      java.util.Stack<BR><BR>它的结果输出为:<BR><BR>public java.lang.Object 
      java.util.Stack.push(java.lang.Object)<BR><BR>public synchronized 
      java.lang.Object java.util.Stack.pop()<BR><BR>public synchronized 
      java.lang.Object java.util.Stack.peek()<BR><BR>public boolean 
      java.util.Stack.empty()<BR><BR>public synchronized int 
      java.util.Stack.search(java.lang.Object)<BR><BR>这样就列出了java.util.Stack 
      类的各方法名以及它们的限制符和返回类型。<BR><BR>这个程序使用 Class.forName 载入指定的类,然后调用 
      getDeclaredMethods 来获取这个类中定义了的方法列表。java.lang.reflect.Methods 
      是用来描述某个类中单个方法的一个类。<BR><BR>2.开始使用 Reflection<BR><BR>用于 reflection 的类,如 
      Method,可以在 java.lang.relfect 包中找到。使用这些类的时候必须要遵循三个步骤:第一步是获得你想操作的类的 
      java.lang.Class 对象。在运行中的 Java 程序中,用 java.lang.Class 
      类来描述类和接口等。<BR><BR>下面就是获得一个 Class 对象的方法之一:<BR><BR>Class c = 
      Class.forName("java.lang.String");<BR><BR>这条语句得到一个 String 
      类的类对象。还有另一种方法,如下面的语句:<BR><BR>Class c = int.class;<BR><BR>或者<BR><BR>Class c 
      = Integer.TYPE;<BR><BR>它们可获得基本类型的类信息。其中后一种方法中访问的是基本类型的封装类 (如 Integer) 
      中预先定义好的 TYPE 字段。<BR><BR>第二步是调用诸如 getDeclaredMethods 
      的方法,以取得该类中定义的所有方法的列表。<BR><BR>一旦取得这个信息,就可以进行第三步了——使用 reflection API 
      来操作这些信息,如下面这段代码:<BR><BR>Class c = 
      Class.forName("java.lang.String");<BR><BR>Method m[] = 
      c.getDeclaredMethods();<BR><BR>System.out.println(m[0].toString());<BR><BR>它将以文本方式打印出 
      String 中定义的第一个方法的原型。<BR><BR>在下面的例子中,这三个步骤将为使用 reflection 
      处理特殊应用程序提供例证。<BR><BR>模拟 instanceof 操作符<BR><BR>得到类信息之后,通常下一个步骤就是解决关于 Class 
      对象的一些基本的问题。例如,Class.isInstance 方法可以用于模拟 instanceof 操作符:<BR><BR>class A 
      {<BR>}<BR><BR>public class instance1 {<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("A");<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean b1 = cls.isInstance(new 
      Integer(37));<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println(b1);<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;boolean b2 = cls.isInstance(new A());<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;System.out.println(b2);<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>在这个例子中创建了一个 A 类的 Class 对象,然后检查一些对象是否是 A 
      的实例。Integer(37) 不是,但 new A() 
      是。<BR><BR>3.找出类的方法<BR><BR>找出一个类中定义了些什么方法,这是一个非常有价值也非常基础的 reflection 
      用法。下面的代码就实现了这一用法:<BR><BR>import java.lang.reflect.*;<BR><BR>public class 
      method1 {<BR>&nbsp; &nbsp;private int f1(Object p, int x) throws 
      NullPointerException {<BR>&nbsp; &nbsp; &nbsp; &nbsp;if (p == 
      null)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new 
      NullPointerException();<BR>&nbsp; &nbsp; &nbsp; &nbsp;return x;<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("method1");<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp;Method methlist[] = 
      cls.getDeclaredMethods();<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for 
      (int i = 0; i &lt; methlist.length; i++) {<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp;Method m = methlist[i];<BR>&nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("name = " + 
      m.getName());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp;System.out.println("decl class = " + 
      m.getDeclaringClass());<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
      &nbsp; &nbsp;Class pvec[] = m.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[] = 
      m.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("return type = " + m.getReturnType());<BR>&nbsp; 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 

⌨️ 快捷键说明

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