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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值