为什么以这种方式使用方法重载 [英] Why Method overloading is used in this way

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

问题描述

我们有2个重载方法



public int Add(int a,int b)

{

返回a + b;

}



public int Add(int a,int b,int c)

{

返回+ b + c;



}



如果我们可以用另一种方式使用方法,请使用方法:



现在看下面:



weh有2个重载方法



public int Addtwo(int a,int b)

{

返回a + b;

}



public int Addthree(int a,int b,int c)

{

返回+ b + c;



}





在我的第二个例子中我们可以做与第一个例子中相同的操作,所以需要重载这个方法,请解释一下。

我曾经面对过这个问题在采访中,但无法回答。

weh have 2 overloaded methods

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

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

}

Why to use Method over loading if we can do it in another way like :

Now Look at below:

weh have 2 overloaded methods

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

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

}


in My second example we can do the same operation as we did in first Example,so what is need of overloading the methon ,Please explain.
I had faced this question in interview,but unable to answer.

推荐答案

问为什么这里是s ame问艺术家你为什么选择这个位置作为你的风景?。因为。或者,甚至更好的答案:为什么不?



使用重载方法不是技术问题,这是风格问题。但最终,这种没有任何功能意义的决定会以负面或积极的方式影响代码的可读性和可维护性。



重载是一个非常误导性的词:没有任何东西被重载,因为这里没有加载。这只是方法名称选择的另一个自由:一些方法可以有相同的名称只是因为从行所在一个被调用,可以根据实际参数传递确定哪一个(有时不可能,然后显示编译错误;并且可以通过精确指定参数类型来解决问题)。这就是全部。



您展示的所有方法都是关于添加的。不幸的是,它们没有任何实际意义,所以批评或批准这种命名是没有多大意义的。还有更多实际案例。看看.NET FCL方法:你会发现很多很好的例子。我建议你考虑一下API在不同命名方面的表现,不管是更好还是更差。从使用角度考虑;例如,如果您改变主意并使用其他方法而不是之前使用的方法,会发生什么。等等。



-SA
Asking "why" here is the same as asking an artist "why have you chosen this location for your landscape?". Because. Or, even better answer: "why not"?

Using "overloaded" methods is not a matter of technology, this is a matter of style. But ultimately, such decision, carrying no functional meaning, affect the readability and maintainability of code, in a negative or a positive way.

"Overloading" is a very misleading word: nothing is "overloaded" because nothing is loaded here. This is nothing but an additional freedom in the choice of method names: some methods can have the same name just because from the line where they one is called, it's possible to figure out which one, based on actual parameter passed (and sometime not possible, then the compilation error is shown; and the problem can be resolved by exact specification of parameter types). That's all.

All the methods you show are about addition. Unfortunately, they don't have any practical sense, so it would not make much sense to criticize or approve this naming. There are many more practical cases. Look at the .NET FCL methods: you will find a lot of good examples. I would just suggest you to think how the API would look with different naming, would it be better or worse. Think in term of usage; what happens, for example, if you change your mind and use another method instead of the one you used before. And so on.

—SA


你提到的是实现结果。我们可以通过不同的方式实现结果。即使不编写方法,我们也可以实现结果。但作为开发人员,您需要牢记开发的软件也应该得到维护。这就是我们在开发过程中使用OOPS,设计模式等所有概念的原因。我知道我没有回答这个问题,但对于任何开发人员来说都是更通用的答案。
What you mention is achieving the result. We can achieve the result in different ways. We can achieve the result even without writing the method. But as a developer you need to keep mind that a developed software should be maintained as well. That's reason we have all this concepts of OOPS, design patterns, etc used during development. I know I have not answered the question but is more generic answer for any developer.


重载是针对代码可读性可维护性&它是名为 polymorphism 的OOP概念之一。您可以在网上找到更多详细信息。这里的示例一 - C#方法重载 [ ^ ]



但你可以使用<改进(减少代码) a href =http://msdn.microsoft.com/en-us/library/dd264739.aspx>命名参数和可选参数 [ ^ ]这是很棒的功能。
Overloading is for code readability and maintainability & it's one of the OOP concept called polymorphism. You could find more details in web. Sample one here - C# Method Overloading[^]

But you could improve(reduce code) this using Named and Optional Arguments[^] which is great features.


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

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