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

📄 tij312.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<p>In the preceding example, if <b>Class.forName(&#160;)</b> fails because it can&#146;t find the class you&#146;re trying to load, it will throw a <b>ClassNotFoundException</b> (ideally, exception names tell you just about everything you need to know about the problem). Here, we simply report the problem and move on, but in more sophisticated programs, you might try to fix the problem inside the exception handler. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0482" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading9575"></a>Class literals</h4>
<p>Java provides a second way to produce the reference to the <b>Class</b> object: the <a name="Index897"></a><a name="Index898"></a><i>class literal</i>. In the preceding program this would look like:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Gum.<font color=#0000ff>class</font>;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>which is not only simpler, but also safer since it&#146;s checked at compile time. Because it eliminates the method call, it&#146;s also more efficient. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1844" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Class literals work with regular classes as well as interfaces, arrays, and primitive types. In addition, there&#146;s a standard field called <a name="Index899"></a><b>TYPE</b> that exists for each of the primitive wrapper classes. The <b>TYPE</b> field produces a reference to the <b>Class</b> object for the associated primitive type, such that:<br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="311.999922" colspan="2" rowspan="1" valign="top">
<p class="Table" align="center"><b>... is equivalent to ...</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>boolean.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Boolean.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>char.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Character.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>byte.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Byte.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>short.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Short.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>int.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Integer.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>long.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Long.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>float.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Float.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>double.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Double.TYPE</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="149.333296" colspan="1" rowspan="1" valign="top">
<p align="center"><b>void.class</b><br></p>
</td>
<td width="161.333293" colspan="1" rowspan="1" valign="top">
<p align="center"><b>Void.TYPE</b><br></p>
</td>
</tr>
</table></div>
<p class="TableFollower"><a name="_Toc375545408"></a>My preference is to use the &#147;<b>.class</b>&#148; versions if you can, since they&#146;re more consistent with regular classes. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1845" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775727"></a><a name="Heading9601"></a>Checking before a cast</h3>
<p>So far, you&#146;ve seen RTTI forms including:<br></p>
<ol>
<li>The classic cast; e.g., &#147;<b>(Shape),</b>&#148; which uses RTTI to
make sure the cast is correct. This will throw a <b>ClassCastException</b> if
you&#146;ve performed a bad cast.</li>
<li>The <b>Class</b> object representing the type of your object. The
<b>Class</b> object can be queried for useful run time information. <font
size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1846"
title="Send BackTalk
Comment">Feedback</a></font></li></ol><p>In C++, the classic cast &#147;<b>(Shape)</b>&#148; 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 &#147;type safe downcast.&#148; The reason for the term &#147;downcast&#148; is the historical arrangement of the class hierarchy diagram. If casting a <a name="Index900"></a><a name="Index901"></a><a name="Index902"></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&#146;t</i> know that a <b>Shape</b> is necessarily a <b>Circle</b>, so the compiler doesn&#146;t allow you to perform a downcast assignment without using an explicit cast. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1847" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index903"></a><a name="Index904"></a>There&#146;s a third form of RTTI in Java. This is the keyword <a name="Index905"></a><a name="Index906"></a><b>instanceof</b>, which 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:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(x <font color=#0000ff>instanceof</font> Dog)
  ((Dog)x).bark();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The <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&#146;s important to use <b>instanceof</b> before a downcast when you don&#146;t have other information that tells you the type of the object; otherwise, you&#146;ll end up with a <a name="Index907"></a><a name="Index908"></a><b>ClassCastException</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1848" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Ordinarily, you might be hunting for one type (triangles to turn purple, for example), but you can easily tally <i>all</i> of the objects by using <b>instanceof</b>. Suppose you have a family of <b>Pet</b> classes:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Pet.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Pet {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Dog.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Dog <font color=#0000ff>extends</font> Pet {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Pug.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Pug <font color=#0000ff>extends</font> Dog {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Cat.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Cat <font color=#0000ff>extends</font> Pet {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Rodent.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Rodent <font color=#0000ff>extends</font> Pet {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Gerbil.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Gerbil <font color=#0000ff>extends</font> Rodent {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Hamster.java</font>
<font color=#0000ff>package</font> c10;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Hamster <font color=#0000ff>extends</font> Rodent {} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>In the coming example, we want to keep track of the number of any particular type of <b>Pet</b>, so we&#146;ll need a class that holds this number in an <b>int</b>. You can think of it as a modifiable <b>Integer</b>: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap12_1849" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c10:Counter.java</font>
<font color=#0000ff>package</font> c10;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> Counter {
  <font color=#0000ff>int</font> i;

⌨️ 快捷键说明

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