Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Overloading&Overriding

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

l          overloadingoverriding

l           overloadingoverriding的觀念:

overloading

名字一樣,給不同的條件(參數,多寡或順序不同即可)做不同的事。譬如上班(){},帶參數”SR”時是在執行趕工寫程式,不帶參數時是在執行摸魚。注意,回傳值不同不可代表overload,且會造成compiler error

overriding

名字一樣,且條件(參數及回傳值)也要一模一樣。主要是覆寫掉繼承來的東西。譬如繼承了超認真的做人態度,就一定會變得超認真(預設繼承來是不能選擇的)只要可以做到接觸的比爸媽更多(access modifier要更大)且犯更少的錯(throws exceptions範圍要更小),就懂得用另一種態度來生活。

l           overloadingoverriding的區別:

 

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  subclassaccess modifier不可比superclassaccess modifier小。

n           ruturn typepara list--

u  subclasssuperclassreturn typepara list須相同。

n           throws exceptions—subset

u  subclass所丟出的checked exception範圍不可超過superclass所丟出的exception

n           final methods不可被overridden

n           final class中的所有methods不會被overridden,因為final class不會有subclass

n           private method不會overridden,因為subclass不會繼承superclassprivate members

n           overriding method(subclass)不可以是static method

n           static method不可被non-static method overridden,只可被subclassstatic method shadowed

override superclass時只有method會被覆寫,property不會被覆寫。

来自:【 Garfield 的 SCJP 閱讀筆記 】



相关文章

相关软件