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

📄 chapter11.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
  }
}

<font color=#0000ff>class</font> Gum {
  <font color=#0000ff>static</font> {
    System.out.println(<font color=#004488>"Loading Gum"</font>);
  }
}

<font color=#0000ff>class</font> Cookie {
  <font color=#0000ff>static</font> {
    System.out.println(<font color=#004488>"Loading Cookie"</font>);
  }
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> SweetShop {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    System.out.println(<font color=#004488>"inside main"</font>);
    <font color=#0000ff>new</font> Candy();
    System.out.println(<font color=#004488>"After creating Candy"</font>);
    <font color=#0000ff>try</font> {
      Class.forName(<font color=#004488>"Gum"</font>);
    } <font color=#0000ff>catch</font>(ClassNotFoundException e) {
      e.printStackTrace();
    }
    System.out.println(
      <font color=#004488>"After Class.forName(\"</font>Gum\<font color=#004488>")"</font>);
    <font color=#0000ff>new</font> Cookie();
    System.out.println(<font color=#004488>"After creating Cookie"</font>);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each of the classes <B>Candy</B>,
<B>Gum</B>, and <B>Cookie</B> has a <A NAME="Index1370"></A><B>static</B> clause
that is executed as the class is loaded for the first time. Information will be
printed out to tell you when loading occurs for that class. In
<B>main(&#160;)</B>, the object creations are spread out between print
statements to help detect the time of loading.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A particularly interesting line
is:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Class.forName(<font color=#004488>"Gum"</font>);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This method is a <B>static</B>
member of <B>Class</B> (to which all <B>Class</B> objects belong). A
<B>Class</B> object is like any other object and so you can get and manipulate a
handle to it. (That&#8217;s what the loader does.) One of the ways to get a
handle to the <B>Class</B> object is
<A NAME="Index1371"></A><A NAME="Index1372"></A><B>forName(&#160;)</B>, which
takes a <B>String</B> containing the textual name (watch the spelling and
capitalization!) of the particular class you want a handle for. It returns a
<B>Class</B> handle.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The output of this program for one
JVM is:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>inside main
Loading Candy
After creating Candy
Loading Gum
After Class.forName(<font color=#004488>"Gum"</font>)
Loading Cookie
After creating Cookie</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that each <B>Class</B>
object is loaded only when it&#8217;s needed, and the <B>static</B>
initialization is performed upon class loading.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Interestingly enough, a different
JVM yielded:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Loading Candy
Loading Cookie
inside main
After creating Candy
Loading Gum
After Class.forName(<font color=#004488>"Gum"</font>)
After creating Cookie</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It appears that this JVM
anticipated the need for <B>Candy</B> and <B>Cookie</B> by examining the code in
<B>main(&#160;)</B>, but could not see <B>Gum</B> because it was created by a
call to <B>forName(&#160;)</B> and not through a more typical call to
<B>new</B>. While this JVM produces the desired effect because it does get the
classes loaded before they&#8217;re needed, it&#8217;s uncertain whether the
behavior shown is precisely correct.</FONT><BR></P></DIV>
<A NAME="Heading354"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Class literals</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In Java 1.1<A NAME="Index1373"></A>
you have a second way to produce the handle to the <B>Class</B> object: use the
<A NAME="Index1374"></A><A NAME="Index1375"></A><I>class literal</I>. In the
above program this would look like:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Gum.<font color=#0000ff>class</font>;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">which is not only simpler, but also
safer since it&#8217;s checked at compile time. Because it eliminates the method
call, it&#8217;s also more efficient.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Class literals work with regular
classes as well as interfaces, arrays, and primitive types. In addition,
there&#8217;s a standard field called <A NAME="Index1376"></A><B>TYPE</B> that
exists for each of the primitive wrapper classes. The <B>TYPE</B> field produces
a handle to the <B>Class</B> object for the associated primitive type, such
that:</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=185 COLSPAN=2 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="CENTER"><FONT FACE="Georgia">... is equivalent to
...</FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">boolean.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Boolean.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">char.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Character.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">byte.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Byte.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">short.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Short.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">int.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Integer.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">long.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Long.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">float.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Float.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">double.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Double.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">void.class</FONT></TT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">Void.TYPE</FONT></TT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545408"></A><A NAME="_Toc408018647"></A></TABLE></P></DIV>
<A NAME="Heading355"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Checking before a cast</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">So far, you&#8217;ve seen RTTI
forms including:</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Georgia">	The classic cast, e.g.
&#8220;<B>(Shape),</B>&#8221; which uses RTTI to make sure the cast is correct
and throws a <B>ClassCastException</B> if you&#8217;ve performed a bad
cast.</FONT><LI><FONT FACE="Georgia">	The <B>Class</B> object representing
the type of your object. The <B>Class</B> object can be queried for useful
runtime information.</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In
C++, the classic cast &#8220;<B>(Shape)</B>&#8221; does <I>not </I>perform RTTI.
It simply tells the compiler to treat the object as the new type. In Java, which
does perform the type check, this cast is often called a &#8220;type safe
downcast<A NAME="Index1377"></A><A NAME="Index1378"></A><A NAME="Index1379"></A>.&#8221;
The reason for the term &#8220;downcast&#8221; is the historical arrangement of
the class hierarchy diagram. If casting a <B>Circle</B> to a <B>Shape</B> is an
upcast, then casting a <B>Shape</B> to a <B>Circle</B> is a downcast. However,
you know a <B>Circle</B> is also a <B>Shape</B>, and the compiler freely allows
an upcast assignment, but you <I>don&#8217;t</I> know that a <B>Shape</B> is
necessarily a <B>Circle</B>, so the compiler doesn&#8217;t allow you to perform
a <A NAME="Index1380"></A><A NAME="Index1381"></A>downcast assignment without
using an explicit cast. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;s a third form of RTTI
in Java. This is the <A NAME="Index1382"></A><A NAME="Index1383"></A>keyword
<B>instanceof</B> that tells you if an object is an instance of a particular
type. It returns a <B>boolean </B>so you use it in the form of a question, like
this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(x <font color=#0000ff>instanceof</font> Dog)
  ((Dog)x).bark();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The above <B>if</B> statement
checks to see if the object <B>x</B> belongs to the class <B>Dog</B>
<I>before</I> casting <B>x</B> to a <B>Dog</B>. It&#8217;s important to use
<B>instanceof</B> before a downcast when you don&#8217;t have other information
that tells you the type of the object; otherwise you&#8217;ll end up with a
<A NAME="Index1384"></A><A NAME="Index1385"></A><B>ClassCastException</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Ordinarily, you might be hunting
for one type (triangles to turn purple, for example), but the following program
shows how to tally <I>all</I> of the objects using
<B>instanceof</B>.</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: PetCount.java</font>
<font color=#009900>// Using instanceof</font>
<font color=#0000ff>package</font> c11.petcount;
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>class</font> Pet {}
<font color=#0000ff>class</font> Dog <font color=#0000ff>extends</font> Pet {}
<font color=#0000ff>class</font> Pug <font color=#0000ff>extends</font> Dog {}

⌨️ 快捷键说明

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