﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Asktalk-随笔分类-JavaSE</title><link>http://www.blogjava.net/asktalk/category/2814.html</link><description>&lt;span class="STYLE3"&gt;天行健，君子以自强不息！&lt;/span&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 12:32:30 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 12:32:30 GMT</pubDate><ttl>60</ttl><item><title>Java语法探究——从多层内部类/嵌套的类中访问外部类的成员</title><link>http://www.blogjava.net/asktalk/archive/2006/02/19/31535.html</link><dc:creator>Asktalk</dc:creator><author>Asktalk</author><pubDate>Sun, 19 Feb 2006 11:24:00 GMT</pubDate><guid>http://www.blogjava.net/asktalk/archive/2006/02/19/31535.html</guid><wfw:comment>http://www.blogjava.net/asktalk/comments/31535.html</wfw:comment><comments>http://www.blogjava.net/asktalk/archive/2006/02/19/31535.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/asktalk/comments/commentRss/31535.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asktalk/services/trackbacks/31535.html</trackback:ping><description><![CDATA[<H5 style="MARGIN: 14pt 0cm 14.5pt"><FONT size=5><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">从多层内部类</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt"><FONT face=宋体>/</FONT></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">嵌套的类中访问外部类的成员</SPAN><SPAN lang=EN-US style="mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></FONT></H5>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本文用到名词说明</SPAN><SPAN lang=EN-US><FONT face=宋体>:<o:p></o:p></FONT></SPAN></STRONG></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-WEIGHT: normal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">内部类：</SPAN></STRONG><STRONG><SPAN lang=EN-US style="FONT-WEIGHT: normal; mso-bidi-font-weight: bold"><FONT face=宋体>Inner Class<o:p></o:p></FONT></SPAN></STRONG></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-WEIGHT: normal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">嵌套类：</SPAN></STRONG><SPAN lang=EN-US style="FONT-FAMILY: Arial; mso-bidi-font-weight: bold">Static Nested Class </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-weight: bold; mso-bidi-font-family: Arial">（与标题上的嵌套的类不同，此处是专有名词）</SPAN><SPAN lang=EN-US style="FONT-FAMILY: Arial; mso-bidi-font-weight: bold"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">静态内部类：同嵌套类一个意思，一物两个名字而已。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN lang=EN-US style="FONT-WEIGHT: normal; mso-bidi-font-weight: bold"><o:p><FONT face=宋体>&nbsp;</FONT></o:p></SPAN></STRONG></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在下面，我们看到对于</SPAN><FONT face=宋体><SPAN lang=EN-US>B</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类来说他的嵌套层次是：</SPAN></FONT><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">AcessMethod-</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">〉</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A-</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">〉</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B </SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">那么访问方法如下：</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt"><CODE><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #7f0055">class&nbsp;</SPAN></B></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B&nbsp;{</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">void&nbsp;</SPAN></B><SPAN style="COLOR: black">h()&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">System.out.println(</SPAN><SPAN style="COLOR: #2a00ff">"h()"</SPAN><SPAN style="COLOR: black">);</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">g();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">f();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">AcessMethod</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">和</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">来说对Ｂ，都是透明的，直接调用。</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">嵌套类实例化的顺序：有外到里！</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">AcessMethod-</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">〉</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A-</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">〉</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B<o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">调用代码如下：</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">AcessMethod&nbsp;am&nbsp;=&nbsp;</SPAN></CODE><CODE><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #7f0055">new&nbsp;</SPAN></B></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">AcessMethod();</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">AcessMethod.A&nbsp;a&nbsp;=&nbsp;am.</SPAN><B><SPAN style="COLOR: #7f0055">new&nbsp;</SPAN></B><SPAN style="COLOR: black">A();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">AcessMethod.A.B&nbsp;b&nbsp;=&nbsp;a.</SPAN><B><SPAN style="COLOR: #7f0055">new&nbsp;</SPAN></B><SPAN style="COLOR: black">B();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">b.h();<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这里很有意思的一点，</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">’.new’</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语法产生了正确的作用域。</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p>&nbsp;</o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 24pt"><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">疑问：</SPAN></STRONG><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">下面我们来看一下在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B.h()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中怎样调用</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">方法？</SPAN></CODE><CODE><B><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-ansi-font-size: 10.5pt"><o:p></o:p></SPAN></B></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p>&nbsp;</o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">下面我们来看是否可以在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中调用</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">方法，在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B.h()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">方法中原则上可以这样调用</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">但是</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">没有实例化，所以这样是错误的，也就是在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中无法调用</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中的非静态方法了。</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN lang=EN-US><o:p><FONT face=宋体>&nbsp;</FONT></o:p></SPAN></STRONG></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">假设：</SPAN></STRONG><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">如果</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">为</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">static</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型，那么在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">B</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中可以这样调用</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">种的方法：</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.this.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">；</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black"><o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><B><SPAN lang=EN-US style="FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 12.0pt; mso-ansi-font-size: 10.5pt"><o:p>&nbsp;</o:p></SPAN></B></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">但是这是不可能的，因为在</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">A1.g()</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">不可能定义为</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">static</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型，详细看下面的问题</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">1</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">（</SPAN></CODE><STRONG><SPAN style="FONT-WEIGHT: normal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">问题</SPAN></STRONG><FONT face=宋体><STRONG><SPAN lang=EN-US style="FONT-WEIGHT: normal; mso-bidi-font-weight: bold">1:</SPAN></STRONG><STRONG><SPAN style="FONT-WEIGHT: normal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">在什么情况下可以定义</SPAN></STRONG><STRONG><SPAN lang=EN-US style="FONT-WEIGHT: normal; mso-bidi-font-weight: bold">static </SPAN></STRONG><STRONG><SPAN style="FONT-WEIGHT: normal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-weight: bold">方法），这是一个缺点吧，但是可以使用嵌套类来解决了。</SPAN></STRONG></FONT><B><SPAN lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f5fbf">/**</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*&nbsp;</SPAN></CODE></SPAN><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">从多层嵌套类中访问外部类的成员</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f5fbf">.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">“</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f5fbf">.new</SPAN></CODE><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">“语法产生了正确的作用域</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*/</SPAN></CODE><BR><CODE><B><SPAN style="COLOR: #7f0055">public&nbsp;class&nbsp;</SPAN></B><SPAN style="COLOR: black">AcessMethod&nbsp;{</SPAN></CODE><BR><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">private&nbsp;void&nbsp;</SPAN></B><SPAN style="COLOR: black">f()&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">System.out.println(</SPAN><SPAN style="COLOR: #2a00ff">"f()"</SPAN><SPAN style="COLOR: black">);</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">class&nbsp;</SPAN></B><SPAN style="COLOR: black">A1&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">private&nbsp;void&nbsp;</SPAN></B><SPAN style="COLOR: black">g()&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">System.out.println(</SPAN><SPAN style="COLOR: #2a00ff">"A<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:chmetcnv UnitName="g" SourceValue="1" HasSpace="False" Negative="False" NumberType="1" TCSC="0" w:st="on">1 </st1:chmetcnv>- g()"</SPAN><SPAN style="COLOR: black">);</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15pt; mso-char-indent-count: 1.5"><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">}<o:p></o:p></SPAN></CODE></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 15pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">class&nbsp;</SPAN></B><SPAN style="COLOR: black">A&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">private&nbsp;void&nbsp;</SPAN></B><SPAN style="COLOR: black">g()&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">System.out.println(</SPAN><SPAN style="COLOR: #2a00ff">"g()"</SPAN><SPAN style="COLOR: black">);</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">class&nbsp;</SPAN></B><SPAN style="COLOR: black">B&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">void&nbsp;</SPAN></B><SPAN style="COLOR: black">h()&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">System.out.println(</SPAN><SPAN style="COLOR: #2a00ff">"h()"</SPAN><SPAN style="COLOR: black">);</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">g();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">f();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">public&nbsp;static&nbsp;void&nbsp;</SPAN></B><SPAN style="COLOR: black">main(String[]&nbsp;args)&nbsp;{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">AcessMethod&nbsp;am&nbsp;=&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">new&nbsp;</SPAN></B><SPAN style="COLOR: black">AcessMethod();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">AcessMethod.A&nbsp;a&nbsp;=&nbsp;am.</SPAN><B><SPAN style="COLOR: #7f0055">new&nbsp;</SPAN></B><SPAN style="COLOR: black">A();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">AcessMethod.A.B&nbsp;b&nbsp;=&nbsp;a.</SPAN><B><SPAN style="COLOR: #7f0055">new&nbsp;</SPAN></B><SPAN style="COLOR: black">B();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">b.h();</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><CODE><SPAN style="COLOR: black">}<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">运行结果：</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">h()</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 50pt; TEXT-ALIGN: left; mso-char-indent-count: 5.0; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">g()</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 50pt; mso-char-indent-count: 5.0"><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">f()</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 50pt; mso-char-indent-count: 5.0"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">问题</SPAN><SPAN lang=EN-US><FONT face=宋体>1:</FONT></SPAN></STRONG><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在什么情况下可以定义</SPAN><SPAN lang=EN-US><FONT face=宋体>static </FONT></SPAN></STRONG><STRONG><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法</SPAN></STRONG><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">？</SPAN><SPAN lang=EN-US><o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><o:p><FONT face=宋体>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">只有在顶层类中定义，或者在静态内部类中定义，看下面的例子</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><CODE><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #7f0055">public&nbsp;class&nbsp;</SPAN></B></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">Test&nbsp;{</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">static&nbsp;void&nbsp;</SPAN></B><SPAN style="COLOR: black">t(){}</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">class&nbsp;</SPAN></B><SPAN style="COLOR: black">T2{</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #3f7f5f">//!</SPAN></CODE></SPAN><CODE><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">错误，</SPAN></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f">The&nbsp;method&nbsp;a&nbsp;cannot&nbsp;be&nbsp;declared&nbsp;static;&nbsp;</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #3f7f5f">//static&nbsp;methods&nbsp;can&nbsp;only&nbsp;be&nbsp;declared&nbsp;in&nbsp;a&nbsp;static&nbsp;or&nbsp;top&nbsp;level&nbsp;type</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #3f7f5f">//static&nbsp;void&nbsp;a(){}</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}<o:p></o:p></SPAN></CODE></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 10.05pt; mso-char-indent-count: 1.0"><CODE><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #7f0055">static</SPAN></B></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: white">&nbsp;</SPAN></CODE><CODE><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #7f0055">class&nbsp;</SPAN></B></CODE><CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black">T3{</SPAN></CODE><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">static&nbsp;void</SPAN></B><SPAN style="COLOR: #3f7f5f">&nbsp;</SPAN><SPAN style="COLOR: black">a(){}</SPAN></CODE><BR><CODE><SPAN style="COLOR: white">&nbsp;&nbsp;</SPAN><SPAN style="COLOR: black">}</SPAN></CODE><BR><CODE><SPAN style="COLOR: black">}</SPAN></CODE></SPAN></P><img src ="http://www.blogjava.net/asktalk/aggbug/31535.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asktalk/" target="_blank">Asktalk</a> 2006-02-19 19:24 <a href="http://www.blogjava.net/asktalk/archive/2006/02/19/31535.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CVS团队开发：Window2003版</title><link>http://www.blogjava.net/asktalk/archive/2005/08/14/10059.html</link><dc:creator>Asktalk</dc:creator><author>Asktalk</author><pubDate>Sat, 13 Aug 2005 23:14:00 GMT</pubDate><guid>http://www.blogjava.net/asktalk/archive/2005/08/14/10059.html</guid><wfw:comment>http://www.blogjava.net/asktalk/comments/10059.html</wfw:comment><comments>http://www.blogjava.net/asktalk/archive/2005/08/14/10059.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/asktalk/comments/commentRss/10059.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/asktalk/services/trackbacks/10059.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: CVS团队开发：Window2003版用法&nbsp;&nbsp;<a href='http://www.blogjava.net/asktalk/archive/2005/08/14/10059.html'>阅读全文</a><img src ="http://www.blogjava.net/asktalk/aggbug/10059.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/asktalk/" target="_blank">Asktalk</a> 2005-08-14 07:14 <a href="http://www.blogjava.net/asktalk/archive/2005/08/14/10059.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>