为什么我应该重载方法? [英] Why should I ever overload methods?

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

问题描述

我在我的重载方法书中发现了两个例子,但它并没有清楚地解释为什么它很有用:

 包裹保持; 
public class main {
public static void main(String [] args)
{
int newScore = calculateScore(Tim,500);
System.out.println(New Score is+ newScore);
calculateScore(75);

$ b public static int calculateScore(String playerName,int score){
System.out.println(Player+ playerName +has score+ score);
回报分数* 1000;
}
public static int calculateScore(int score){
System.out.println(Unnamed player scored+ score +points);
回报分数* 1000;






$ b

这很简单,但说实话,它似乎很没用在这里重写方法重载,似乎只是为了这样做。



本书中的下一个例子是方法重载,这似乎更有用,因为该程序计算英尺到厘米,并且有一种方法可以放入英尺和英寸,还有一种方法可以放置英寸。然而,为这个做两个单独的方法似乎仍然很容易。



这就是说,这样做有没有真正的好处? (我阅读了这个,但我并不是很满意,似乎只是因为容易做出新的方法。)

解决方案

我想如果你谈论函数/方法重载的真正好处,你不会绕过你的路,那么就像你在你的问题中指出的那样,你不会找到任何东西。

但它有帮助吗?让我们来看看这个例子。



假设我正在创建一个应用程序,通过他的名字找到一个人,然后我声明并定义一个方法

  public Person [] findPerson(String name)

现在我们得到一个要求,我们必须根据他的出生日期找到一个人,所以引入一个新方法

  public Person [] findPerson_byDOB(Date date)

假设这个过程继续,

  public Person [] findPerson(String name)
public Person [] findPerson_byDOB(Date日期)
public Person [] findPerson_byAddress(Address address)
public Person [] findPerson_bySSN(SSN ssn)
public Person [] findPerson_byDepartment(Department department)
public Person [] findPerson_byMobile (Mobile mobile)

这只是一个部分;这可以继续,当我们被要求引入多个参数,如

  public Person [] findPerson_byMobileAndAddress(Mobile mobile,Address address) 
public Person [] findPerson_byAddressAndDepartment(地址,部门部门)
public Person [] findPerson_byDOBAndDepartment(DOB dob,Department,department)
public Person [] findPerson_byAddressAndDOB(Address address,DOB dob)

以及更多...

虽然这看起来可能有些夸张,但请相信我,在制定实际的行业级应用程序时,我们可能会遇到类似上百种和数百种方法的情况,最终我们需要一个所有这些方法的目录他们实际上做了什么。



这实际上是一个噩梦,当我们不得不使用它时,必须找到所有这些方法的名称。 然而,当所有参数不同时,我们可以给函数赋予相同的名称,它真的变得非常容易记住。

  public Person [] findPerson(String name)
public Person [] findPerson(Date date)
public person [] findPerson(Address address)
public Person [] findPerson(SSN ssn)
public Person [] findPerson(Department department)
public Person [] findPerson(Mobile mobile)
public Person [] findPerson(Mobile mobile,Address address)
public Person [] findPerson(Address address,Department department)
public Person [] findPerson(DOB dob,Department,department)
public person [] findPerson(Address address,DOB dob)

现在David在他的回答,我们都知道如何获得整数的 String 值;可能我们已经在某处读过它了。

  static String.valueOf(new Integer(1)); 

但是你知道还有多少方法是用相同的名字重载吗?

  static String.valueOf(boolean b)
static String.valueOf(char c)
static String.valueOf( char [] data)
static String.valueOf(double d)
static String.valueOf(float f)
static String.valueOf(int i)
static String.valueOf( long l)
static String.valueOf(Object obj)

好处是你不用没有必要记住他们。您甚至不必猜测,因为它一直都是同名。

根据Namnodorel的建议,



考虑这个重载方法 PrintStream class。 p>

  void println()
void println(boolean x)
void println(char x)
void println(char x)
void println(double x)
void println(float x)
void println(int x)
void println(long x)
void println(Object x)
void println(String x)

如果我们必须写:

  void println_emptyLine()
void println_boolean(boolean x)
void println_character(char x)
void println_characterArray(char [] x)
void println_double(double x)
void println_float(float x)
void println_integer(int x)
void println_long(long x)
void println_object(Object x)
void println_string(String x)


I found two examples in my book of overloading methods, but it doesn't explain clearly exactly why it's useful:

package keepo;
public class Main{
    public static void main(String [] args)
    {
        int newScore = calculateScore("Tim", 500);
        System.out.println("New Score is" + newScore);
        calculateScore(75);

    }
    public static int calculateScore(String playerName, int score){
        System.out.println("Player" + playerName +"Has score" + score);
        return score * 1000;
    }
    public static int calculateScore(int score){
        System.out.println("Unnamed player scored" + score + "points");
        return score * 1000;
    }
}

This is pretty straightforward, but honestly it seems pretty useless to method overload here, and it seems just doing it for the sake of doing it.

The next example in the book does method overloading which seems a bit more useful, because that program calculates feet to centimeters, and there is one method where you can put in feet and inches, and one method where you can put inches. However, it still seems just as easy to make two separate methods for this.

That being said, are there any real benefits to doing this? (I read this, but I am not really to satisfied. It seems just as easy to make new methods.)

解决方案

I think if you talk about the real benefits of function/method overloading, something without which you won't get your way around, then as you've pointed out in your question, you won't find any.

But how is it helpful? Let's consider this example.

Let's suppose that I'm making an application that finds a person by his name and I declare and define a method

public Person[] findPerson(String name)

Now we get a requirement where we've to find a person by his Date of Birth, so introduce a new method

public Person[] findPerson_byDOB(Date date)

Let's suppose this continues and we've this many methods in my application.

public Person[] findPerson(String name)
public Person[] findPerson_byDOB(Date date)
public Person[] findPerson_byAddress(Address address)
public Person[] findPerson_bySSN(SSN ssn)
public Person[] findPerson_byDepartment(Department department)
public Person[] findPerson_byMobile(Mobile mobile)

It's just one part; this can carry on when we are asked to introduce multiple parameters, like

public Person[] findPerson_byMobileAndAddress(Mobile mobile, Address address)
public Person[] findPerson_byAddressAndDepartment(Address address, Department department)
public Person[] findPerson_byDOBAndDepartment(DOB dob, Department, department)
public Person[] findPerson_byAddressAndDOB(Address address, DOB dob)

and many many more...

While this may seem a little bit exaggerated, trust me, when making an actual industry level application, we may come across a situation when we get hundreds and hundreds of methods like this, and ultimately we'll need a catalog of all these methods of what they actually do.

It is actually a nightmare when we'll have to find the name of all these methods when we would have to use it.

However, when all the parameters are different, we can give same name to the function and it really becomes very easy to remember.

public Person[] findPerson(String name)
public Person[] findPerson(Date date)
public Person[] findPerson(Address address)
public Person[] findPerson(SSN ssn)
public Person[] findPerson(Department department)
public Person[] findPerson(Mobile mobile)
public Person[] findPerson(Mobile mobile, Address address)
public Person[] findPerson(Address address, Department department)
public Person[] findPerson(DOB dob, Department, department)
public Person[] findPerson(Address address, DOB dob)

Now as David pointed out in his answer, we all know how to get String value of integer; probably we have read it somewhere.

static String.valueOf(new Integer(1));

But do you know how many more methods are there that are overloaded with the same name?

static String.valueOf(boolean b)
static String.valueOf(char c)
static String.valueOf(char[] data)
static String.valueOf(double d)
static String.valueOf(float f)
static String.valueOf(int i)
static String.valueOf(long l)
static String.valueOf(Object obj)

The benefits are that you don't have to memorize them all. You don't even have to guess because it's the same name all the way.


EDIT as per Namnodorel's advice

Consider this overloaded method of PrintStream class.

void println()
void println(boolean x)
void println(char x)
void println(char[] x)
void println(double x)
void println(float x)
void println(int x)
void println(long x)
void println(Object x)
void println(String x)

Just think about the readability if we had to write:

void println_emptyLine()
void println_boolean(boolean x)
void println_character(char x)
void println_characterArray(char[] x)
void println_double(double x)
void println_float(float x)
void println_integer(int x)
void println_long(long x)
void println_object(Object x)
void println_string(String x)

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

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