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

📄 right3_2_5.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="95%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top">             <p> <span class="pt9-black"> </span><span class="zhongdian">1. 内部类的定义和使用:<a name="01"></a></span><span class="pt9-black"><br>              <br>                内部类是在一个类的内部嵌套定义的类,它可以是其它类的成员,也可以在一个语句块的内部定义,还可以在表达式内部匿名定义。<br>                内部类有如下特性:<br>              <br>                ◇ 一般用在定义它的类或语句块之内,在外部引用它时必须给出完整的名称.名字不能与包含它的类名相同。<br>                ◇ 可以使用包含它的类的静态和实例成员变量,也可以使用它所在方法的局部变量。<br>                ◇ 可以定义为abstract。<br>                ◇ 可以声明为private或protected。<br>                ◇ 若被声明为static,就变成了顶层类,不能再使用局部变量。<br>                ◇ 若想在Inner Class中声明任何static成员,则该Inner Class必须声明为static。<br>              <br>                例3-8 是一个说明内部类如何使用的例子,其中,定义了两个内部类:MouseMotionHandler和MouseEventHandler,分别用来处理鼠标移动事件和鼠标点按事件。<br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题">【例3-8】<br>                import java.awt.*;<br>                import java.awt.event.*;<br>                public class TwoListenInner {<br>                  private Frame f;<br>                  private TextField tf;<br>                    public static void main(String args[]) {<br>                     TwoListenInner that=new TwoListenInner();<br>                     that.go(); <br>                  }<br>              <br>                    public void go() {<br>                     f=new Frame(&quot;Two listeners example&quot;);<br>                     f.add(&quot;North&quot;,new Label(&quot;Click and drag the               mouse&quot;));<br>                     tf=new TextField(30);<br>                     f.add(&quot;South&quot;,tf);<br>                     f.addMouseMotionListener(new MouseMotionHandler());<br>                     f.addMouseListener(new MouseEventHandler());<br>                     f.setSize(300,300);<br>                     f.setVisible(true);<br>                  } <br>                  public class MouseMotionHandler extends MouseMotionAdapter {               <br>                    public void mouseDragged(MouseEvent e){<br>                     String s=&quot;Mouse dragging:X=&quot;+e.getX()+&quot;Y=&quot;+e.getY();<br>                     tf.setText(s);<br>                    }<br>                  }<br>                  public class MouseEventHandler extends MouseAdapter {<br>                    public void mouseEntered(MouseEvent e){<br>                     String s=&quot;The mouse entered&quot;;<br>                     tf.setText(s);<br>                    }<br>                    public void mouseExited(MouseEvent e){<br>                     String s=&quot;The mouse left the building&quot;;<br>                     tf.setText(s);<br>                    }<br>                  }<br>                }<br>              <br>                <font color="#000099">同学们可以运行一下这个程序,看一看它的运行结果。当你将鼠标移入frame时,文本框中会出现:&quot;The               mouse entered&quot;;当你在frame中拖曳鼠标时,文本框中会出现:&quot;Mouse dragging:X=64               Y=117&quot;;当鼠标离开文本框时,文本框中出现:&quot;The mouse left the building&quot;。<br>              <br>               </font></span><span class="zhongdian">2. 匿名类的定义和使用:<a name="02"></a></span><span class="pt9-black"><br>              <br>                匿名类是一种特殊的内部类,它是在一个表达式内部包含一个完整的类定义。通过对例6-7中go()部分语句的修改,我们可以看到匿名类的使用情况。<br>              <br>                public void go() {<br>                       f=new Frame(&quot;Two listeners example&quot;);<br>                       f.add(&quot;North&quot;,new Label(&quot;Click and drag               the mouse&quot;));<br>                       tf=new TextField(30);<br>                       f.add(&quot;South&quot;,tf);<br>                       f.addMouseMotionListener(new MouseMotionHandler(){<br>                       <font color="339900">/*定义了一个匿名类,类名没有显式地给出,只是该类是<br>                        MouseMotionHandler类的子类*/</font><br>                         public void mouseDragged(MouseEvent e){<br>                           String s=&quot;Mouse dragging:X=&quot;+e.getX()+&quot;Y<br>                           =&quot;+e.getY();<br>                           tf.setText(s);<br>                         }<font color="#000099"><br>              </font>         });<br>                       f.addMouseListener(new MouseEventHandler());<br>              <br>                       f.setSize(300,300);<br>                       f.setVisible(true);<br>                       }<font color="#000099"><br>              <br>               </font></span><span class="zhongdian">3. 内部类的优缺点:<a name="03"></a></span><span class="pt9-black"><font color="#000099"><br>              <br>                </font>◇ 优点:节省编译后产生的字节码文件的大小<br>                ◇ 缺点:使程序结构不清楚</span></p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

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