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

📄 chapter04.html

📁 java 是一个很好的网络开发环境。由于它是通过解释的方法
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Demotion {
  <font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) { 
    System.out.println(s); 
  }

  <font color=#0000ff>void</font> f1(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f1(char)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f1(byte)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f1(short)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f1(int)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f1(long)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f1(float)"</font>); }
  <font color=#0000ff>void</font> f1(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f1(double)"</font>); }

  <font color=#0000ff>void</font> f2(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f2(char)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f2(byte)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f2(short)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f2(int)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f2(long)"</font>); }
  <font color=#0000ff>void</font> f2(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f2(float)"</font>); }

  <font color=#0000ff>void</font> f3(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f3(char)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f3(byte)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f3(short)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f3(int)"</font>); }
  <font color=#0000ff>void</font> f3(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f3(long)"</font>); }

  <font color=#0000ff>void</font> f4(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f4(char)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f4(byte)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f4(short)"</font>); }
  <font color=#0000ff>void</font> f4(<font color=#0000ff>int</font> x) { prt(<font color=#004488>"f4(int)"</font>); }

  <font color=#0000ff>void</font> f5(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f5(char)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f5(byte)"</font>); }
  <font color=#0000ff>void</font> f5(<font color=#0000ff>short</font> x) { prt(<font color=#004488>"f5(short)"</font>); }

  <font color=#0000ff>void</font> f6(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f6(char)"</font>); }
  <font color=#0000ff>void</font> f6(<font color=#0000ff>byte</font> x) { prt(<font color=#004488>"f6(byte)"</font>); }

  <font color=#0000ff>void</font> f7(<font color=#0000ff>char</font> x) { prt(<font color=#004488>"f7(char)"</font>); }

  <font color=#0000ff>void</font> testDouble() {
    <font color=#0000ff>double</font> x = 0;
    prt(<font color=#004488>"double argument:"</font>);
    f1(x);f2((<font color=#0000ff>float</font>)x);f3((<font color=#0000ff>long</font>)x);f4((<font color=#0000ff>int</font>)x);
    f5((<font color=#0000ff>short</font>)x);f6((<font color=#0000ff>byte</font>)x);f7((<font color=#0000ff>char</font>)x);
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Demotion p = <font color=#0000ff>new</font> Demotion();
    p.testDouble();
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here, the methods take narrower
primitive values. If your argument is wider then you must
<A NAME="Index280"></A><I>cast</I> to the necessary type using the type name in
parentheses. If you don&#8217;t do this, the compiler will issue an error
message.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You should be aware that this is a
<A NAME="Index281"></A><A NAME="Index282"></A><I>narrowing conversion,</I> which
means you might lose information during the cast. This is why the compiler
forces you to do it &#8211; to flag the narrowing conversion.
</FONT><A NAME="_Toc408018480"></A><BR></P></DIV>
<A NAME="Heading141"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Overloading on return
values<BR><A NAME="Index283"></A><A NAME="Index284"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It is common to wonder &#8220;Why
only class names and method argument lists? Why not distinguish between methods
based on their return values?&#8221; For example, these two methods, which have
the same name and arguments, are easily distinguished from each
other:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>void</font> f() {}</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><TT><FONT FACE="Courier New">int f()
{}</FONT></TT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This works fine when the compiler
can unequivocally determine the meaning from the context, as in <B>int x =
f(&#160;)</B>. However, you can call a method and ignore the return value; this
is often referred to as <I>calling a method for its <A NAME="Index285"></A>side
effect</I> since you don&#8217;t care about the return value but instead want
the other effects of the method call. So if you call the method this
way:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>f();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">how can Java determine which
<B>f(&#160;)</B> should be called? And how could someone reading the code see
it? Because of this sort of problem, you cannot use return value types to
distinguish overloaded
methods.</FONT><A NAME="_Toc375545279"></A><A NAME="_Toc408018481"></A><BR></P></DIV>
<A NAME="Heading142"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Default constructors</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As mentioned previously, a default
constructor <A NAME="Index286"></A><A NAME="Index287"></A>is one without
arguments, used to create a &#8220;vanilla object.&#8221; If you create a class
that has no constructors, the compiler will automatically create a default
constructor for you. For example:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: DefaultConstructor.java</font>

<font color=#0000ff>class</font> Bird {
  <font color=#0000ff>int</font> i;
}

<font color=#0000ff>public</font> <font color=#0000ff>class</font> DefaultConstructor {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    Bird nc = <font color=#0000ff>new</font> Bird(); <font color=#009900>// default!</font>
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The line</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>new</font> Bird();</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">creates a new object and calls the
default constructor, even though one was not explicitly defined. Without it we
would have no method to call to build our object. However, if you define any
constructors (with or without arguments), the compiler will <I>not</I>
synthesize one for you:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Bush {
  Bush(<font color=#0000ff>int</font> i) {}
  Bush(<font color=#0000ff>double</font> d) {}
}</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now if you say:</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&#8217;s as if when you don&#8217;t
put in any constructors, the compiler says &#8220;You are bound to need
<I>some</I> constructor, so let me make one for you.&#8221; But if you write a
constructor, the compiler says &#8220;You&#8217;ve written a constructor so you
know what you&#8217;re doing; if you didn&#8217;t put in a default it&#8217;s
because you meant to leave it
out.&#8221;</FONT><A NAME="_Toc375545280"></A><A NAME="_Toc408018482"></A><BR></P></DIV>
<A NAME="Heading143"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The <A NAME="Index288"></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(&#160;)</B> for both those objects:</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&#8217;s only one method
called <B>f(&#160;)</B>, how can that method know whether it&#8217;s being
called for the object <B>a</B> or <B>b</B>? </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 &#8220;send a message to an
object,&#8221; the compiler does some undercover work for you. There&#8217;s a
secret first argument passed to the method <B>f(&#160;)</B>, and that argument
is the handle to the object that&#8217;s being manipulated. So the two method
calls above become something like:</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&#8217;t write these expressions and get the compiler to accept them, but it
gives you an idea of what&#8217;s happening.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Suppose you&#8217;re inside a
method and you&#8217;d like to get the handle to the current object. Since that
handle is passed <I>secretly</I> by the compiler, there&#8217;s no identifier
for it. However, for this purpose there&#8217;s a keyword: <B>this</B>. The
<B>this</B> keyword &#8211; which can be used only inside a method &#8211;
produces the handle to the object the method has been called for. You can treat
this handle just like any other object handle. Keep in mind that if you&#8217;re
calling a method of your class from within another method of your class, you
don&#8217;t need to use <B>this;</B> you simply call the method. The current
<B>this</B> handle is automatically used for the other method. Thus you can
say:</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(&#160;)</B>, you
<I>could</I> say <B>this.pick(&#160;)</B> but there&#8217;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 handle to the
current object. For example, it&#8217;s often used in <B>return</B> statements
when you want to return the handle to the current object:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: 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>private</font> <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(&#160;)</B>
returns the handle to the current object via the <B>this</B> keyword, multiple
operations can easily be performed on the same object.</FONT><BR></P></DIV>
<A NAME="Heading144"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Calling constructors from constructors<BR><A NAME="Index289"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When you write several constructors
for a class, there are times when you&#8217;d like to call one constructor from
another to avoid duplicating code. You can do this using the <B>this</B>
keyword. </FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Normally, when you say <B>this</B>,
it is in the sense of &#8220;this object&#8221; or &#8220;the current
object,&#8221; and by itself it produces the handle 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:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Flower.java</font>

⌨️ 快捷键说明

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