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

📄 tij304.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> ATypeName { <font color=#009900>/* Class body goes here */</font> }</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This introduces a new type, although the class body consists only of a comment (the stars and slashes and what is inside, which will be discussed later in this chapter), so there is not too much that you can do with it. However, you can create an object of this type using <b>new</b>:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>ATypeName a = <font color=#0000ff>new</font> ATypeName();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>But you cannot tell it to do much of anything (that is, you cannot send it any interesting messages) until you define some methods for it. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_398" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545226"></a><a name="_Toc24775552"></a><a name="Heading1390"></a>Fields
and methods</h3>
<p>When you define a class (and all you do in Java is define classes, make objects of those classes, and send messages to those objects), you can put two types of elements in your class: <i>fields</i> (sometimes called data members), and <i>methods</i> (sometimes called <i>member functions</i>). A field is an object of any type that you can communicate with via its reference. It can also be one of the primitive types (which isn&#146;t a reference). If it is a reference to an object, you must initialize that reference to connect it to an actual object (using <b>new</b>, as seen earlier) in a special method called a <i>constructor</i> (described fully in Chapter 4). If it is a primitive type, you can initialize it directly at the point of definition in the class. (As you&#146;ll see later, references can also be initialized at the point of definition.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_399" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Each object keeps its own storage for its fields; the fields are not shared among objects. Here is an example of a class with some fields: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_400" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> DataOnly {
  <font color=#0000ff>int</font> i;
  <font color=#0000ff>float</font> f;
  <font color=#0000ff>boolean</font> b;
}</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>This class doesn&#146;t <i>do</i> anything, but you can create an object: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_401" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>DataOnly d = <font color=#0000ff>new</font> DataOnly();</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You can assign values to the fields, but you must first know how to refer to a member of an object. This is accomplished by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_402" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>objectReference.member</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_403" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>d.i = 47;
d.f = 1.1f; <font color=#009900>// &#145;f&#146; after number indicates float constant</font>
d.b = <font color=#0000ff>false</font>;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>It is also possible that your object might contain other objects that contain data you&#146;d like to modify. For this, you just keep &#147;connecting the dots.&#148; For example: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_404" title="Send BackTalk Comment">Feedback</a></font><br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>myPlane.leftTank.capacity = 100;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The <b>DataOnly </b>class cannot do much of anything except hold data, because it has no methods. To understand how those work, you must first understand <i>arguments</i> and <i>return values</i>, which will be described shortly. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_405" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h4>
<a name="Heading1414"></a>Default values for primitive members</h4>
<p>When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it:<br></p>
<div align="center" style="position:relative; left: 0"><table border="1">
<tr valign="top">
<th width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Primitive type</b><br></p>
</th>
<th width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>Default</b><br></p>
</th>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>boolean</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>false</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>char</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>&#145;\u0000&#146; (null)</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>byte</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>(byte)0</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>short</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>(short)0</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>int</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>0</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>long</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>0L</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>float</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>0.0f</b><br></p>
</td>
</tr>
<tr valign="top">
<td width="133.333300" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>double</b><br></p>
</td>
<td width="141.333298" colspan="1" rowspan="1" valign="top">
<p class="Table"><b>0.0d</b><br></p>
</td>
</tr>
</table></div>
<p class="TableFollower">Note carefully that the default values are what Java guarantees when the variable is used <i>as a member of a class</i>. This ensures that member variables of primitive types will always be initialized (something C++ doesn&#146;t do), reducing a source of bugs. However, this initial value may not be correct or even legal for the program you are writing. It&#146;s best to always explicitly initialize your variables. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_406" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>This guarantee doesn&#146;t apply to &#147;local&#148; variables&#151;those that are not fields of a class. Thus, if within a method definition you have:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> x;</PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Then <b>x</b> will get some arbitrary value (as in C and C++); it will not automatically be initialized to zero. You are responsible for assigning an appropriate value before you use <b>x</b>. If you forget, Java definitely improves on C++: you get a compile-time error telling you the variable might not have been initialized. (Many C++ compilers will warn you about uninitialized variables, but in Java these are errors.) <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_407" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h2>
<a name="_Toc375545227"></a><a name="_Toc24775553"></a><a name="Heading1439"></a>Methods,
arguments, <br>and return values</h2>
<p>In many languages (like C and C++), the term <i>function</i> is used to describe a named subroutine. The term that is more commonly used in Java is <i>method,</i> as in &#147;a way to do something.&#148; If you want, you can continue thinking in terms of functions. It&#146;s really only a syntactic difference, but this book follows the common Java usage of the term &#147;method.&#148; <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_408" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Methods in Java determine the messages an object can receive. In this section you will learn how simple it is to define a method. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap02_409" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The fundamental parts of a method are the name, the arguments, the return type, and the body. Here is the basic form:<br></p>

⌨️ 快捷键说明

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