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

📄 j-scjp-5-1.html

📁 网页式教程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td class="hbg" height="21" width="160"></td><td class="bbg" valign="top" height="21" colspan="2">  <a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/" class="mainlink">IBM 主页</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/products/index.shtml" class="mainlink">产品与服务</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/support/index.shtml" class="mainlink">支持与下载</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/account/index.shtml" class="mainlink">个性化服务</a></td>
</tr>
</table>
<TABLE width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font size="-1" face="Verdana, sans-serif"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/cn/index.shtml">developerWorks 中国网站</a> : <a href="/developerWorks/cn/java/index.shtml">java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle?OpenView&Count=500">教学 - 在线教程</a></font>
<br>
<img alt=" Java 认证成功之路,第 1 部分:SCJP " src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-scjp.zip"><img alt="下载 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
					
					
					
&nbsp;&nbsp;&nbsp;
				</td>
</tr>
<tr>
<td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-5-2.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="5.语言基础 " src="imagemaster/titlebar5.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15">&nbsp;</td><td bgcolor="ffffff" width="12">&nbsp;</td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>包和类声明 </b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr>   第 1 页(共6 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>一个 <i>包(package)</i> 表示一组类。 <code>package </code>语句应该是源文件中第一个有效的语句。如果没有 <code>package </code>语句,那么源文件中的类就属于默认的未命名包,或者属于命名的包。一个源文件中只允许有一个 <code>package</code> 语句。 </p>
<p>在一个源文件中,只能有一个公有类,文件名应与该类名相匹配。 <code>import</code> 语句使您可以直接使用类而不是用其完全限定名引用它。 <code>Import</code> 语句必须在 <code>package </code>语句之后和类声明之前,如下所示: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  package com.abc;          // package statement
  import java.net.*;        // wild card import (imports all classes in the package)
  import java.util.Vector;  // class import (only the given class is imported)
</code>
</pre>
<p>
						
<b>嵌套的类声明 </b>
						
<br>
一个 <i>嵌套类 </i>或者 <i>内部类 </i>是在另一个类中定义的类。嵌套类可以是非静态的、方法本地的、匿名的或者静态的。 </p>
<p>
						
<b>非静态的内部类 </b>
						
<br>
非静态的内部类定义不使用 <code>static</code> 关键词(有关关键词的更多信息请参阅  <a href="j-scjp-5-3.html">Java 关键词和标识符 </a>)。它是在其外围类的范围内定义的,如下所示: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  class MyClass
  {
    class MyInner{}
  }
</code>
</pre>
<p>要实例化一个非静态的内部类,需要外部类的一个实例。 </p>
<p>非静态的内部类可以访问其外部类的所有成员。在外部实例代码中,只用内部类名来实例化它: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  MyInner myInner = new MyInner();
</code>
</pre>
<p>在外部实例代码之外,内部类名必须包括在外部实例中: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  MyClass myClass = new MyClass();
  MyClass.MyInner inner = myClass.new MyInner();
</code>
</pre>
<p>适用于非静态内部类的修饰符有 <code>final</code>、 <code>abstract</code>、 <code>public</code>、 <code>private</code> 和 <code>protected</code>。</p>
<p>
						
<b>方法本地的内部类 </b>
						
<br>
方法本地的内部类可以在定义它的方法内部实例化。它还可以在构造函数、本地块、静态初始化程序或者实例初始化程序中。它不能访问外围方法的局部变量,除非它们是 <code>final</code> 的。这种类仅有的修饰符是 <code>abstract</code> 或者 <code>final</code>。</p>
<p>
						
<b>匿名的内部类 </b>
						
<br>
匿名的内部类是没有名字的内部类。它们可以扩展一个类或者实现一个实例。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  button1.addMouseListener(new MouseAdapter()
  {
    public void mouseClicked(MouseEvent e) 
    {
      button1_mouseClicked(e);
    }
  });
</code>
</pre>
<p>这个例子显示了一个匿名的内部类。一定要注意这个类是在一个方法参数中定义的。 </p>
<p>
						
<b>静态的嵌套类 </b>
						
<br>
静态的嵌套类是外围类的一个静态成员。它不能访问这个类的实例变量和方法。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
  class MyOuter
  {      
    static class MyNested{}
  }
  class Test
  {
    public static void main(String args[]){
    MyOuter.MyNested n = new MyOuter.MyNested();
    }
  }
</code>
</pre>
<p>正如这个例子所显示的,静态的嵌套类不需要外部类的一个实例来用于实例化。 </p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-5-2.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>

⌨️ 快捷键说明

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