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

📄 chap02.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I42' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I43>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each object keeps its own storage for its
data members; the data members are not shared among objects. Here is an example
of a class with some data members:

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

<backtalk:display ID=TIJ3_CHAPTER2_I44>
</FONT><BR></P></DIV>

<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>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This class doesn&#8217;t <I>do</I>
anything, but you can create an object:

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

<backtalk:display ID=TIJ3_CHAPTER2_I45>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>DataOnly d = <font color=#0000ff>new</font> DataOnly();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can assign values to the data
members, 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:

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

<backtalk:display ID=TIJ3_CHAPTER2_I46>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>objectReference.member</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For example:

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

<backtalk:display ID=TIJ3_CHAPTER2_I47>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>d.i = 47;
d.f = 1.1f;
d.b = <font color=#0000ff>false</font>;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is also possible that your object
might contain other objects that contain data you&#8217;d like to modify. For
this, you just keep &#8220;connecting the dots.&#8221; For example:

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

<backtalk:display ID=TIJ3_CHAPTER2_I48>
</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>myPlane.leftTank.capacity = 100;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>DataOnly </B>class cannot do much
of anything except hold data, because it has no member functions (methods). To
understand how those work, you must first understand <I>arguments</I> and
<I>return values</I>, which will be described shortly.

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

<backtalk:display ID=TIJ3_CHAPTER2_I49>
</FONT><BR></P></DIV>
<A NAME="Heading97"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Default values for primitive members</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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:</FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><TABLE BORDER>
<TR VALIGN="TOP">
<TH WIDTH=100 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Primitive type</B></FONT><BR></P></DIV>
</TH>
<TH WIDTH=106 COLSPAN=1 ROWSPAN=1 VALIGN="TOP">
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Default</B></FONT><BR></P></DIV>
</TH>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>boolean</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>false</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>char</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>&#8216;\u0000&#8217;
(null)</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>byte</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>(byte)0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>short</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>(short)0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>int</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>long</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0L</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>float</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0.0f</B></FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>double</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>0.0d</B></FONT><BR></P></DIV>
</TD>
</TR>
</TABLE></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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&#8217;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&#8217;s best to always explicitly initialize your variables. 

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

<backtalk:display ID=TIJ3_CHAPTER2_I50>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This guarantee doesn&#8217;t apply to
&#8220;local&#8221; variables&#8212;those that are not fields of a class. Thus,
if within a function definition you have:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> x;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">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.)

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

<backtalk:display ID=TIJ3_CHAPTER2_I51>
</FONT><A NAME="_Toc375545227"></A><A NAME="_Toc481064520"></A><BR></P></DIV>
<A NAME="Heading98"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Methods, arguments, <BR>and return values</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Up until now, the term <I>function</I>
has been used to describe a named subroutine. The term that is more commonly
used in Java is <I>method,</I> as in &#8220;a way to do something.&#8221; If you
want, you can continue thinking in terms of functions. It&#8217;s really only a
syntactic difference, but from now on &#8220;method&#8221; will be used in this
book rather than &#8220;function.&#8221;

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

<backtalk:display ID=TIJ3_CHAPTER2_I52>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Methods in Java determine the messages an
object can receive. In this section you will learn how simple it is to define a
method. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER2_I52' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER2_I53>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The fundamental parts of a method are the
name, the arguments, the return type, and the body. Here is the basic
form:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>returnType methodName( <font color=#009900>/* Argument list */</font> ) {
  <font color=#009900>/* Method body */</font>
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The return type is the type of the value
that pops out of the method after you call it. The argument list gives the types
and names for the information you want to pass into the method. The method name
and argument list together uniquely identify the method. 

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

<backtalk:display ID=TIJ3_CHAPTER2_I54>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Methods in Java can be created only as
part of a class. A method can be called only for an
object,</FONT><A NAME="fnB21" HREF="#fn21">[21]</A><FONT FACE="Georgia"> and
that object must be able to perform that method call. If you try to call the
wrong method for an object, you&#8217;ll get an error message at compile-time.
You call a method for an object by naming the object followed by a period (dot),
followed by the name of the method and its argument list, like this:
<B>objectName.methodName(arg1, arg2, arg3)</B>. For example, suppose you have a
method <B>f(&#160;)</B> that takes no arguments and returns a value of type
<B>int</B>. Then, if you have an object called <B>a</B> for which
<B>f(&#160;)</B> can be called, you can say this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> x = a.f();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The type of the return value must be
compatible with the type of <B>x</B>.

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

<backtalk:display ID=TIJ3_CHAPTER2_I55>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This act of calling a method is commonly
referred to as <I>sending a message to an object</I>. In the above example, the
message is <B>f(&#160;)</B> and the object is <B>a</B>. Object-oriented
programming is often summarized as simply &#8220;sending messages to
objects.&#8221;

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

<backtalk:display ID=TIJ3_CHAPTER2_I56>
</FONT><A NAME="_Toc375545228"></A><A NAME="_Toc481064521"></A><BR></P></DIV>
<A NAME="Heading99"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The argument list</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The method argument list specifies what
information you pass into the method. As you might guess, this
information&#8212;like everything else in Java&#8212;takes the form of objects.
So, what you must specify in the argument list are the types of the objects to
pass in and the name to use for each one. As in any situation in Java where you
seem to be handing objects around, you are actually passing
references</FONT><A NAME="fnB22" HREF="#fn22">[22]</A><FONT FACE="Georgia">. The

⌨️ 快捷键说明

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