overload

同一個詞可能有不同的意思——在java中將這種具有同個方法名的多個方法稱爲 重載

例如:類的無參構造,我們可以重載該構造方法,讓他帶有參數。

    重載的方法返回類型必須都一致,只有參數個數或類型不同。(例子:--1-->)

    值得注意的是:在重載的過程中,參數列表的順序不同也會被視作是不同的方法。(例子:--3-->)

在實際應用中,當我們需要同一個方法在必要的時候傳入不同類型的參數的時候我們就可以使用重載的方法,在同一個類中書寫多個同名的方法。(例子:--2-->)


Often, the same word expresses a number of different meanings--it's overloaded.

Most human languages are the redundant, so even if you miss a few words, you can still determine the meaning. You don't need unique identifiers--you can deduce meaning from context.

Most programming languages(C in particular) require you to have a unique identifier for each method( often called functions in those languages). So you could not have one called print() for printing integers and another called print() for printing floats -- each function requires a unique name.

In Java, another factor forces the overloading of method names: constructor.

If you create two constructors: no-args constructor(default constructor) and constructor with int as an argument. Both are constructors, so they must have the same name--the name of the class. Thus, method overloading is essential to allow the same method name to be used with different argument types. And although method overloading is a must for constructors, it's a general convenience and can be used with any method.

--1--> eg:    

    public class Tree{

                Tree() {}    // no-args constructor

                Tree(int initialHeight){}    // constructor with int as an argument

                void info() {}       // no-args method

                void info(String s) {}        // overloaded method

        }

Application scene: Distinguish overloaded methods.

If you want to create a method and user can pass the different type arguments into the method while required, you could do these as follows.

--2--> eg: 

public class ParameterOverloading {

            void f1(int x) { coding.... }

            void f1(char x) { coding... }

            void f1(String x) { coding... }

            ··· ···

}

NOTE: Something you need to pay attenttion to  is that even differences in the ordering of arguments are sufficient to distinguish two methods, although you don't normally want to take this approach because it produces difficult-to-maintain code.

--3--> eg:

public class OverloadOrder {

        void f(String s, int i) {    System.out.println("String: " + s + ", int: " + i);    }

        void f(int i,String s) {    System.out.println("int: " + i + ", String: " + s);    }

        public static void main(String[] args){

            f("String first", 11);

            f(99, "Int First");    

        }

}

/* OUTPUT */

String: String first, int: 11

int: 99, String: Int First

Never do these as follows, although they have the same name and arguments, are easily distinguished from each other :

void f() {}

int f() { return 1; }

Just because if you call the method f() , how can java determine which f() should be called? And how could someone reading the code see it? 

Because if this sort of problem, you cannot use return value types to distinguish overloaded methods.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章