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

📄 chap12.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I9' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I10>
</FONT><A NAME="_Toc312374135"></A><A NAME="_Toc375545406"></A><A NAME="_Toc481064774"></A><BR></P></DIV>
<A NAME="Heading403"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The Class object</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To understand how RTTI works in Java, you
must first know how type information is represented at run-time. This is
accomplished through a special kind of object called the
<A NAME="Index1476"></A><A NAME="Index1477"></A><A NAME="Index1478"></A><I>Class
object,</I> which contains information about the class. (This is sometimes
called a <A NAME="Index1479"></A><A NAME="Index1480"></A><I>meta-class.</I>) In
fact, the <B>Class</B> object is used to create all of the &#8220;regular&#8221;
objects of your class. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I10' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I11>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There&#8217;s a <B>Class</B> object for
each class that is part of your program. That is, each time you write and
compile a new class, a single <B>Class</B> object is also created (and stored,
appropriately enough, in an identically named <B>.class </B>file). At run-time,
when you want to make an object of that class, the
<A NAME="Index1481"></A><A NAME="Index1482"></A>Java Virtual Machine (JVM)
that&#8217;s executing your program first checks to see if the <B>Class</B>
object for that type is loaded. If not, the JVM loads it by finding the
<B>.class </B>file with that name. Thus, a Java program isn&#8217;t completely
loaded before it begins, which is different from many traditional languages.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I11' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Once the <B>Class</B> object for that
type is in memory, it is used to create all objects of that type.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I12' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If this seems shadowy or if you
don&#8217;t really believe it, here&#8217;s a demonstration program to prove
it:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c12:SweetShop.java</font>
<font color=#009900>// Examination of the way the class loader works.</font>

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

<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.err);
    }
    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> have a <A NAME="Index1483"></A><B>static</B>
clause that is executed as the class is loaded for the first time. Information
will be printed 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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I13' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I14>
</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 reference to it.
(That&#8217;s what the loader does.) One of the ways to get a reference to the
<B>Class</B> object is
<A NAME="Index1484"></A><A NAME="Index1485"></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 reference for. It returns a
<B>Class</B> reference. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I14' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I15>
</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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I15' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I16>
</FONT><BR></P></DIV>
<A NAME="Heading404"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Class literals</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Java provides a second way to produce the
reference to the <B>Class</B> object, using a
<A NAME="Index1486"></A><A NAME="Index1487"></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.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER12_I16' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER12_I17>
</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="Index1488"></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:</FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=234 COLSPAN=2 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>... is equivalent to
...</B></FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>boolean.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Boolean.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>char.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Character.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>byte.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Byte.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>short.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Short.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>int.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Integer.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>long.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Long.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>float.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Float.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>double.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Double.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>void.class</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><B>Void.TYPE</B></FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc375545408"></A></TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">My preference is to use the
&#8220;<B>.class</B>&#8221; versions if you can, since they&#8217;re more
consistent with regular classes.

⌨️ 快捷键说明

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