overriding 和overloading之间有什么差别
来源:百度知道 编辑:UC知道 时间:2024/07/07 21:41:53
overloading与overriding的观念:
overloading
名字一样,给不同的条件(参数,多寡或顺序不同即可)做不同的事。譬如上班(){},带参数”SR”时是在执行”赶工写程式”,不带参数时是在执行”摸鱼”。注意,回传值不同不可代表overload,且会造成compiler error。
overriding
名字一样,且条件(参数及回传值)也要一模一样。主要是覆写掉继承来的东西。譬如继承了超认真的做人态度,就一定会变得超认真(预设继承来是不能选择的)只要可以做到接触的比爸妈更多(access modifier要更大)且犯更少的错(throws exceptions范围要更小),就懂得用另一种态度来生活。
l overloading与overriding的区别:
meaning
Access modifier
Return
type
Method
name
Para list
Throws
exceptions
overloading
reuse the method name
dont’ care
dont’ care
同
不同(type, count,order)
dont’ care
overriding
redefine the method
不小
同
同
同
subset
l overriding需特别注意是否满足下列条件,否则会compiler error。
n access modifier--不小:
u subclass的access modifier不可比superclass的access modifier小。
n ruturn type、para list--同
u subclass与sup