方法重载是多态形式还是其他形式? [英] Is method overloading a form of polymorphism or something else?

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

问题描述

我有一个长期的疑问.有人可以告诉我方法重载是多态的一种形式还是完全不同的东西?

I have a long standing doubt. Could someone please tell me whether method overloading is a form of polymorphism or is it something completely different?

推荐答案

方法重载只是一种语法糖,使您可以使用名称相同但参数不同的方法.它与多态无关.方法重载通常用于定义两个接受不同参数的方法,例如:

Method overloading is just a syntax sugar allowing you to have methods with same name but different arguments. It has nothing to do with polymorphism. Method overloading is typically used to define two methods accepting different arguments, e.g.:

public void println(boolean x) //...
public void println(char x) //...

或跳过某些参数并使用一些默认值:

or to skip certain parameters and use some defaults:

public String substring(int beginIndex) //...
public String substring(int beginIndex, int endIndex) //...

另一方面,

方法重载是继承的基础,并且与多态性密切相关.

Method overriding, on the other hand, is a foundation of inheritance and is more closely related to polymorphism.

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

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