📄 chapter04.html
字号:
both overloaded constructors and overloaded ordinary methods:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Overloading.java</font>
<font color=#009900>// Demonstration of both constructor</font>
<font color=#009900>// and ordinary method overloading.</font>
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>class</font> Tree {
<font color=#0000ff>int</font> height;
Tree() {
prt(<font color=#004488>"Planting a seedling"</font>);
height = 0;
}
Tree(<font color=#0000ff>int</font> i) {
prt(<font color=#004488>"Creating new Tree that is "</font>
+ i + <font color=#004488>" feet tall"</font>);
height = i;
}
<font color=#0000ff>void</font> info() {
prt(<font color=#004488>"Tree is "</font> + height
+ <font color=#004488>" feet tall"</font>);
}
<font color=#0000ff>void</font> info(String s) {
prt(s + <font color=#004488>": Tree is "</font>
+ height + <font color=#004488>" feet tall"</font>);
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) {
System.out.println(s);
}
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Overloading {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < 5; i++) {
Tree t = <font color=#0000ff>new</font> Tree(i);
t.info();
t.info(<font color=#004488>"overloaded method"</font>);
}
<font color=#009900>// Overloaded constructor:</font>
<font color=#0000ff>new</font> Tree();
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>Tree</B> object can be created
either as a seedling, with no argument, or as a plant grown in a nursery, with
an existing height. To support this, there are two constructors, one that takes
no arguments (we call constructors that take no arguments
<A NAME="Index274"></A><A NAME="Index275"></A><I>default
constructors</I></FONT><A NAME="fnB17" HREF="#fn17">[17]</A><A NAME="Index276"></A><A NAME="Index277"></A><FONT FACE="Georgia">)
and one that takes the existing height.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might also want to call the
<B>info( )</B> method in more than one way. For example, with a
<B>String</B> argument if you have an extra message you want printed, and
without if you have nothing more to say. It would seem strange to give two
separate names to what is obviously the same concept. Fortunately, method
overloading allows you to use the same name for
both.</FONT><A NAME="_Toc375545277"></A><A NAME="_Toc408018478"></A><BR></P></DIV>
<A NAME="Heading139"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Distinguishing overloaded
methods<BR><A NAME="Index278"></A><A NAME="Index279"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If the methods have the same name,
how can Java know which method you mean? There’s a simple rule: Each
overloaded method must take a unique list of argument types.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you think about this for a
second, it makes sense: how else could a programmer tell the difference between
two methods that have the same name, other than by the types of their
arguments?</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Even differences in the ordering of
arguments is sufficient to distinguish two methods: (Although you don’t
normally want to take this approach, as it produces difficult-to-maintain
code.)</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: OverloadingOrder.java</font>
<font color=#009900>// Overloading based on the order of</font>
<font color=#009900>// the arguments.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> OverloadingOrder {
<font color=#0000ff>static</font> <font color=#0000ff>void</font> print(String s, <font color=#0000ff>int</font> i) {
System.out.println(
<font color=#004488>"String: "</font> + s +
<font color=#004488>", int: "</font> + i);
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> print(<font color=#0000ff>int</font> i, String s) {
System.out.println(
<font color=#004488>"int: "</font> + i +
<font color=#004488>", String: "</font> + s);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
print(<font color=#004488>"String first"</font>, 11);
print(99, <font color=#004488>"Int first"</font>);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The two <B>print( )</B>
methods have identical arguments, but the order is different, and that’s
what makes them
distinct.</FONT><A NAME="_Toc312373866"></A><A NAME="_Toc375545278"></A><A NAME="_Toc408018479"></A><BR></P></DIV>
<A NAME="Heading140"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Overloading with primitives</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Primitives can be automatically
promoted from a smaller type to a larger one and this can be slightly confusing
in combination with overloading. The following example demonstrates what happens
when a primitive is handed to an overloaded method:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: PrimitiveOverloading.java</font>
<font color=#009900>// Promotion of primitives and overloading</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> PrimitiveOverloading {
<font color=#009900>// boolean can't be automatically converted</font>
<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>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> f2(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f2(double)"</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> f3(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f3(float)"</font>); }
<font color=#0000ff>void</font> f3(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f3(double)"</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> f4(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f4(long)"</font>); }
<font color=#0000ff>void</font> f4(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f4(float)"</font>); }
<font color=#0000ff>void</font> f4(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f4(double)"</font>); }
<font color=#0000ff>void</font> f5(<font color=#0000ff>long</font> x) { prt(<font color=#004488>"f5(long)"</font>); }
<font color=#0000ff>void</font> f5(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f5(float)"</font>); }
<font color=#0000ff>void</font> f5(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f5(double)"</font>); }
<font color=#0000ff>void</font> f6(<font color=#0000ff>float</font> x) { prt(<font color=#004488>"f6(float)"</font>); }
<font color=#0000ff>void</font> f6(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f6(double)"</font>); }
<font color=#0000ff>void</font> f7(<font color=#0000ff>double</font> x) { prt(<font color=#004488>"f7(double)"</font>); }
<font color=#0000ff>void</font> testConstVal() {
prt(<font color=#004488>"Testing with 5"</font>);
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
}
<font color=#0000ff>void</font> testChar() {
<font color=#0000ff>char</font> x = 'x';
prt(<font color=#004488>"char argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testByte() {
<font color=#0000ff>byte</font> x = 0;
prt(<font color=#004488>"byte argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testShort() {
<font color=#0000ff>short</font> x = 0;
prt(<font color=#004488>"short argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testInt() {
<font color=#0000ff>int</font> x = 0;
prt(<font color=#004488>"int argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testLong() {
<font color=#0000ff>long</font> x = 0;
prt(<font color=#004488>"long argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testFloat() {
<font color=#0000ff>float</font> x = 0;
prt(<font color=#004488>"float argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>void</font> testDouble() {
<font color=#0000ff>double</font> x = 0;
prt(<font color=#004488>"double argument:"</font>);
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
PrimitiveOverloading p =
<font color=#0000ff>new</font> PrimitiveOverloading();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testFloat();
p.testDouble();
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you view the output of this
program, you’ll see that the constant value 5 is treated as an <B>int</B>,
so if an overloaded method is available that takes an <B>int</B> it is used. In
all other cases, if you have a data type that is smaller than the argument in
the method, that data type is promoted. <B>char</B> produces a slightly
different effect, since if it doesn’t find an exact <B>char</B> match, it
is promoted to <B>int</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">What happens if your argument is
<I>bigger</I> than the argument expected by the overloaded method? A
modification of the above program gives the answer:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: Demotion.java</font>
<font color=#009900>// Demotion of primitives and overloading</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -