📄 chap04.htm
字号:
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now if you say:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I31'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I32>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>new</font> Bush();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">the compiler will complain that it cannot
find a constructor that matches. It’s as if when you don’t put in
any constructors, the compiler says “You are bound to need <I>some</I>
constructor, so let me make one for you.” But if you write a constructor,
the compiler says “You’ve written a constructor so you know what
you’re doing; if you didn’t put in a default it’s because you
meant to leave it out.”
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I32'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I33>
</FONT><A NAME="_Toc375545280"></A><A NAME="_Toc481064575"></A><BR></P></DIV>
<A NAME="Heading170"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The <A NAME="Index406"></A>this keyword</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you have two objects of the same type
called <B>a</B> and <B>b</B>, you might wonder how it is that you can call a
method <B>f( )</B> for both those objects:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I33'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I34>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Banana { <font color=#0000ff>void</font> f(<font color=#0000ff>int</font> i) { <font color=#009900>/* ... */</font> } }
Banana a = <font color=#0000ff>new</font> Banana(), b = <font color=#0000ff>new</font> Banana();
a.f(1);
b.f(2);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If there’s only one method called
<B>f( )</B>, how can that method know whether it’s being called for
the object <B>a</B> or <B>b</B>?
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I34'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I35>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To allow you to write the code in a
convenient object-oriented syntax in which you “send a message to an
object,” the compiler does some undercover work for you. There’s a
secret first argument passed to the method <B>f( )</B>, and that argument
is the reference to the object that’s being manipulated. So the two method
calls above become something like:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I35'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I36>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>Banana.f(a,1);
Banana.f(b,2);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This is internal and you can’t
write these expressions and get the compiler to accept them, but it gives you an
idea of what’s happening.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I36'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I37>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Suppose you’re inside a method and
you’d like to get the reference to the current object. Since that
reference is passed <I>secretly</I> by the compiler, there’s no identifier
for it. However, for this purpose there’s a keyword: <B>this</B>. The
<B>this</B> keyword—which can be used only inside a method—produces
the reference to the object the method has been called for. You can treat this
reference just like any other object reference. Keep in mind that if
you’re calling a method of your class from within another method of your
class, you don’t need to use <B>this</B>; you simply call the method. The
current <B>this</B> reference is automatically used for the other method. Thus
you can say:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I37'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I38>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Apricot {
<font color=#0000ff>void</font> pick() { <font color=#009900>/* ... */</font> }
<font color=#0000ff>void</font> pit() { pick(); <font color=#009900>/* ... */</font> }
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Inside <B>pit( )</B>, you
<I>could</I> say <B>this.pick( )</B> but there’s no need to. The
compiler does it for you automatically. The <B>this</B> keyword is used only for
those special cases in which you need to explicitly use the reference to the
current object. For example, it’s often used in <B>return</B> statements
when you want to return the reference to the current object:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I38'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I39>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:Leaf.java</font>
<font color=#009900>// Simple use of the "this" keyword.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Leaf {
<font color=#0000ff>int</font> i = 0;
Leaf increment() {
i++;
<font color=#0000ff>return</font> <font color=#0000ff>this</font>;
}
<font color=#0000ff>void</font> print() {
System.out.println(<font color=#004488>"i = "</font> + i);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Leaf x = <font color=#0000ff>new</font> Leaf();
x.increment().increment().increment().print();
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Because <B>increment( )</B> returns
the reference to the current object via the <B>this</B> keyword, multiple
operations can easily be performed on the same object.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I39'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I40>
</FONT><BR></P></DIV>
<A NAME="Heading171"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Calling constructors from constructors<BR><A NAME="Index407"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you write several constructors for a
class, there are times when you’d like to call one constructor from
another to avoid duplicating code. You can do this using the <B>this</B>
keyword.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I40'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I41>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Normally, when you say <B>this</B>, it is
in the sense of “this object” or “the current object,”
and by itself it produces the reference to the current object. In a constructor,
the <B>this</B> keyword takes on a different meaning when you give it an
argument list: it makes an explicit call to the constructor that matches that
argument list. Thus you have a straightforward way to call other constructors:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I41'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I42>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c04:Flower.java</font>
<font color=#009900>// Calling constructors with "this."</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Flower {
<font color=#0000ff>int</font> petalCount = 0;
String s = <font color=#0000ff>new</font> String(<font color=#004488>"null"</font>);
Flower(<font color=#0000ff>int</font> petals) {
petalCount = petals;
System.out.println(
<font color=#004488>"Constructor w</font><font color=#004488>/ int arg only, petalCount= "</font>
+ petalCount);
}
Flower(String ss) {
System.out.println(
<font color=#004488>"Constructor w</font><font color=#004488>/ String arg only, s="</font> + ss);
s = ss;
}
Flower(String s, <font color=#0000ff>int</font> petals) {
<font color=#0000ff>this</font>(petals);
<font color=#009900>//! this(s); // Can't call two!</font>
<font color=#0000ff>this</font>.s = s; <font color=#009900>// Another use of "this"</font>
System.out.println(<font color=#004488>"String & int args"</font>);
}
Flower() {
<font color=#0000ff>this</font>(<font color=#004488>"hi"</font>, 47);
System.out.println(
<font color=#004488>"default constructor (no args)"</font>);
}
<font color=#0000ff>void</font> print() {
<font color=#009900>//! this(11); // Not inside non-constructor!</font>
System.out.println(
<font color=#004488>"petalCount = "</font> + petalCount + <font color=#004488>" s = "</font>+ s);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Flower x = <font color=#0000ff>new</font> Flower();
x.print();
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The constructor <B>Flower(String s, int
petals)</B> shows that, while you can call one constructor using <B>this</B>,
you cannot call two. In addition, the constructor call must be the first thing
you do or you’ll get a compiler error message.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I42'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I43>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This example also shows another way
you’ll see <B>this</B> used. Since the name of the argument <B>s </B>and
the name of the member data <B>s</B> are the same, there’s an ambiguity.
You can resolve it by saying <B>this.s</B> to refer to the member data.
You’ll often see this form used in Java code, and it’s used in
numerous places in this book.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I43'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I44>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>print( )</B> you can see that
the compiler won’t let you call a constructor from inside any method other
than a constructor.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER4_I44'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER4_I45>
</FONT><BR></P></DIV>
<A NAME="Heading172"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
The meaning of static<BR><A NAME="Index408"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">With the <B>this </B>keyword in mind, you
can more fully understand what it means to make a
<A NAME="Index409"></A><A NAME="Index410"></A>method <B>static</B>. It means
that there is no <B>this</B> for that particular method. You cannot call
non-<B>static</B> methods from inside <B>static</B>
methods</FONT><A NAME="fnB28" HREF="#fn28">[28]</A><FONT FACE="Georgia">
(although the reverse is possible), and you can call a <B>static</B> method for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -