当前位置: 代码迷 >> 综合 >> overload
  详细解决方案

overload

热度:13   发布时间:2023-12-16 00:34:15.0

同一个词可能有不同的意思——在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.



  相关解决方案