Java方法重载 [英] Java method overloading

查看:100
本文介绍了Java方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是Java核心的学习者. 我想在这里了解多态性概念.

I am still a learner of core java. I want to understand the polymorphism concepts here.

我已经了解了最重要的内容,并对超载有疑问.

I have understood the overriding and have a question on overloading.

为什么我们称其为方法重载,尽管我们调用的是不同的方法(我是说仅参数不同).

Why do we call it as method overloading though we are calling different methods(i mean only the arguments are different.) .

我只是感觉很简单,因为调用在编译时绑定的不同方法,唯一的区别是我具有相同的方法名称.

I simply feel that it is quite simple as calling different methods which bind during compile time and only difference here is that i have a same method name..

Class A {

    method A(int i){}

    method A(int i, int B){}

}

请分享您的输入.

思考 普尼思

推荐答案

使用方法重载,您将仅使用不同的参数和/或不同的输出来调用相同的方法".这使您可以轻松编写具有相同核心功能但具有不同输入参数的方法.示例:

With method overloading you're calling "the same method", only with different parameters and/or different output. This makes it easy for you to write methods with the same core functionality but with different input parameters. Example:

public int Sum(int a, int b) {
    return a + b;
}

public double Sum (double a, double b) {
    return a + b;
}

否则,您将拥有SumInt(...)和SumDouble(...)之类的方法.您还可以使用2种方法,这些方法具有相同的返回类型但输入不同,并且仍然可以轻松使用重载:

Otherwise you would have methods like SumInt(...) and SumDouble(...) and so on. You can also have 2 methods with the same return type but different input and still use overloading for ease:

public int Sum(int a, int b) {
    return Sum(a, b, 0);
}

public int Sum (int a, int b, int c) {
    return a + b + c;
}

这样,您仅在逻辑上放置了一个一个位置,而所有其他方法仅使用所有逻辑(仅使用不同的参数)调用了一个方法.然后还有构造函数重载.例如,您可以有一个空的构造函数,可以在其中设置一些默认值,也可以有一个构造函数,可以在其中自己设置值:

This way, you only have one place with al the logic and all the other methods just call the one method with all the logic, only with different parameters. And then there's also constructor overloading. For example you can have an empty constructor in which you set some default values and you can have a constructor where you can set the values yourself:

//Without overloading you'll have to set the properties after instantiating the class
public MyClass() {
    this.PropertyOne = "1";
    this.PropertyTwo = 2;
}

//usage:
MyClass instance = new MyClass(); 
//now theproperties are already set to "1" and 2, wheter you like it or not
//here you change the values
instance.PropertyOne = "...";
instance.PropertyTwo = 0;

//With constructor overloading you have this
public MyClass() {
    this("One", 2);
}

public MyClass(string propOne, int propTwo) {
    this.PropertyOne = propOne;
    this.PropertyTwo = propTwo;
}

//usage:
MyClass instance = new MyClass("MyValue", 1000);
//if you call MyClass() the default values STILL will be set :)

第二种方式为您提供更多的可能性,不是吗?而且它使更改代码变得更加容易.希望这会有所帮助!

The second way gives you more possibilities, not? And it makes it a lot more easy to change your code. Hope this helps!

这篇关于Java方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆