Topic: 请教:怎样理解“函数的重载”?

  Print this page

1.请教:怎样理解“函数的重载”? Copy to clipboard
Posted by: WWWLS
Posted on: 2005-10-02 18:15

书中说:“函数的重载要求输入参数必须不一样,即参数的类型、个数或者顺序不一样,输入参数的名字不一样,不能用来构成函数的重载。所以第9行是错误的。” 该程序的运行结果是:
int i = 10
int j = 3.1415926
String i= How are you
-------------------
书中解说让我看得迷糊,
我也看不懂8、9行与33、34、35有什么区别?
诸位学友,谁能帮我解答一下?
--------------------------
1:public class OverLoadingDemo
2: {
3: public static void print(int i)
4:{
5: System.out.println("int i ="+i);
6:}
7:public static void print(int a,String b){
8: print ( a );
9: print ( b );
10:}
11:public static String print(String i)
12:{
13: System.out.println("String i="+i);
14: return i;
15:}
16:public static void print(double j)
17:{
18: System.out.println("int j="+j);
19:}
20:/*
21: public static void print(double i);
22:{
23: System.out.println("int i="+i);
24:}
25:*/
26:public static void main(String arg[])
27:{
28: int a=10;
29: double b =3.1415926;
30: String c="How are you?";
31:
32: print ( a );
33: print ( b );
34: print ( c );
35:
36:}
37: }

2.Re:请教:怎样理解“函数的重载”? [Re: WWWLS] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-10-02 20:50

Java supports method name overloading so that multiple methods can share the same name. For example, suppose you are writing a class that can render various types of data (strings, integers, and so on) to its drawing area. You need to write a method that knows how to render each data type. In other languages, you have to think of a new name for each method, for example, drawString, drawInteger, drawFloat, and so on. In Java, you can use the same name for all of the drawing methods but pass a different type of parameter to each method. So, in your data rendering class, you can declare three methods named draw, each of which takes a different type of parameter:
class DataRenderer {
void draw(String s) {
. . .
}
void draw(int i) {
. . .
}
void draw(float f) {
. . .
}
}

Overloaded methods are differentiated by the number and type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types. You cannot declare more than one method with the same name and the same number and type of arguments because the compiler cannot differentiate them. So, draw(String s) and draw(String t) are identical and result in a compiler error.

这是java tutorial 里面关于overload(重载)的描述,请仔细阅读一下

切记不要和override(覆盖)相混淆

3.Re:请教:怎样理解“函数的重载”? [Re: WWWLS] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-10-02 21:52

Name overloading is actually supported in the computer sciences from the very beginning, and in all programming languages.

The plus (+) operator is overloaded long before even OO concepts came into play in computer sciences. It is in Fortran (the first high level computer language) for sure.

int + int: this plus(+) is passed by 2 ints as parameters.
double + double: this plus(+) is passed by 2 doubles as parameters.

======================

All natural languages are overloaded long before computer even invented.

In Chinese:
运动可以锻炼身体!
分子运动是运动!

Chineses word 运动 is overloaded in these 2 sentences, they actually mean different things. We distingush them by context (上下文)。

Compiler is not that smart as we do. Compiler distinguishes overloaded methods by signature!!!

4.Re:请教:怎样理解“函数的重载”? [Re: WWWLS] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-10-02 22:03

Read more about Methods Overloading and Overriding from here:

http://bobcat.webappcabaret.net/javachina/faq/02.htm#Overload_

5.Re:请教:怎样理解“函数的重载”? [Re: WWWLS] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-10-02 22:31

gongshi给的链接讲解的非常清楚
我刚刚看了一遍,其中这段qa推荐大家能好好的看看,并且能够深入的理解一下
因为下面这段除了涉及到overload以及override之外,在多态性方面也是一个极佳的例子

Q. Can you explain the result of the following example? Oh, my!

class Base {
public boolean foo(Base b) {
return true;
}
}
class Sub extends Base {
public boolean foo(Sub s) {
return false;
}
}

public class Test {
public static void main(String argv[]) {
Base bb = new Base();
Base bs = new Sub();
Sub ss = new Sub();

System.out.println(bb.foo(bb)); //true

System.out.println(bs.foo(bs)); //true ???
System.out.println(bs.foo(ss)); //true ???
System.out.println(bs.foo(bb)); //true ???
System.out.println(ss.foo(bs)); //true ???
System.out.println(ss.foo(bb)); //true ???

System.out.println(ss.foo(ss)); //false
}
}
A: The foo methods are overloaded in the Sub. In Base, there is only one foo method, and it is not overridden by the Sub!
Overloading is fundamentally different then overriding. There is no polymorphism or dynamic binding here!!!
All decisions are made at compile time!!! See detailed explanation in the same code below, documented!

class Base {
// There is only one foo method in Base!!!
public boolean foo(Base b) {
return true;
}
}
class Sub extends Base {
// differnt signature, method overloading
// there are 2 foo methods in the Sub
public boolean foo(Sub s) {
return false;
}
}

public class Test {
public static void main(String argv[]) {
// bb is a Base ref to the compiler, Base obj at runtime
Base bb = new Base();
// bs is a Base ref to the compiler, Sub obj at runtime
Base bs = new Sub();
// ss is a Sub ref to the compiler, Sub obj at runtime
Sub ss = new Sub();

// All these 4 lines are Base ref
// call Base.foo(Base) method, and only one foo available
// bs is a Base ref, and ss ISA Base ref too
// Everything is fine!!!
System.out.println(bb.foo(bb)); //true
System.out.println(bs.foo(bs)); //true
System.out.println(bs.foo(ss)); //true
System.out.println(bs.foo(bb)); //true

// bb, bs are both Base refs to Compiler
// ss is a Sub ref
// call Sub.foo(Base) which is inherited from Base
System.out.println(ss.foo(bs)); //true
System.out.println(ss.foo(bb)); //true

// no doubt about this one, am I right?
System.out.println(ss.foo(ss)); //false
}
}



   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923