有谁能够帮我。我们可以只在同一类或者在dericed类中使用重载 [英] can anybody help me. can we use overloading only in same class or in dericed class also

查看:67
本文介绍了有谁能够帮我。我们可以只在同一类或者在dericed类中使用重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我。我们是否只能在同一个类或dericed类中使用重载

can anybody help me. can we use overloading only in same class or in dericed class also

推荐答案

重载它几乎与继承和OOP无关。





请参阅我们与manchanx的讨论,对此答案发表评论。严格来说,在一些更为微妙的案例中,当在对呼叫陈述的可能不同解释之间选择优先权时,它是相关的。另请参阅引用的文章。

[结束编辑]



许多人对这个不好的术语感到困惑:没有任何东西真正过载,因为什么都没有加载。只要没有要求具有唯一方法名称,只要编译器有一种方法推断在调用行中使用相同名称的方法,通过看看实际的论点。这只是一个方便的问题,有一组方法几乎相同,但基于不同的参数集。只需使用这个逻辑,随意定义几个具有相同名称的方法,无论其他方法在哪里,在同一个类中,在基类中或任何派生类中。



只能以便于开发人员和自我描述的方式进行。不要过于担心编译器:如果你违反规则,它会告诉你并让你有机会修改方法配置文件或名称。在实践中,这太简单了。



-SA
"Overloading" it almost irrelevant to inheritance and OOP in general.


Please see our discussion with manchanx in comments to this answer. Strictly speaking, it is relevant in some more delicate cases, when the priorities in choice between possible different interpretations of the call statement come into play. See also the article referenced.
[END EDIT]

Many have been confused with this bad term: nothing is really "overloaded", because nothing is loaded. This is nothing but the absence of the requirement to have unique method names, as soon as a compiler has a way to infer which of the methods with the same name is meant at the line of the call, by "looking at actual arguments passed". It is just a matter of convenience, having a set of methods doing nearly the same but based on different set of parameter. Simply use this logic and feel free to define several methods with the same name, no matter where the other methods are, in the same class, in a base class or any derived classes.

Only do it the way it would be convenient for developers and self-describing. Don't worry too much about the compiler: if you break the rules, it will "tell" you and give you the chance to fix either the method profiles or the name. In practice, this is way too easy.

—SA


请参阅Sergey的回答重载的一个很好的解释,以及为什么它通常与继承(派生类)没有任何关系。在所有坚持这种情况的99%的情况下你都会安全。



在实践中,可能会出现一些不太清楚的边缘情况(另一种​​情况) 1%)其中一个案例与继承重叠。如果您想阅读,请参阅 C#深度 - 重载 [ ^ ]。它虽然相当先进,也许您只想将它​​加入书签并在收集到更多经验后再回过头来: - )
Please see Sergey's answer for an excellent explanation of overloading and why it normally doesn't have anything to do with inheritance (derived classes). You'll be safe in 99% of all cases sticking to that.

In practice, there can be some edge cases where things aren't so clear (the other 1%) and one of those cases overlaps with inheritance. If you want to read up on that, please refer to C# in Depth - Overloading[^]. It's fairly advanced though and maybe you want to just bookmark it and come back to it once you've gathered a bit more experience :-)


希望这有助于您解决查询问题。



http:/ /stackoverflow.com/questions/16709065/overloading-base-method-in-derived-class [ ^ ]
hi hope this will help you to solve your query.

http://stackoverflow.com/questions/16709065/overloading-base-method-in-derived-class[^]


这篇关于有谁能够帮我。我们可以只在同一类或者在dericed类中使用重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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