




已閱讀5頁,還剩38頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Java語言程序設計 對象和類 抽象數據類型 基本數據類型屬性操作自定義數據類型屬性 數據成員 操作 函數成員 定義方法 函數和子程序voidreturn參數列表 值傳遞和地址傳遞 基本數據類型值傳遞其它數據類型值里存放的是地址地址傳遞 數據隱藏 隱藏的意義正確的訪問封裝 屏蔽實現(xiàn)的細節(jié) 便于改變實現(xiàn)隱藏的實現(xiàn)privatedefaultpublic this引用 指代當前對象獲取當前對象的指針 classVariable intx 0 y 0 z 0 voidinit intx inty this x x this y y intz 5 System out println x x y y z z publicclassVariableTest publicstaticvoidmain String args Variablev newVariable System out println x v x v y v y z v z v init 20 30 System out println x v x v y v y z v z publicclassLeaf privateinti 0 Leafincrement i returnthis voidprint System out println i i publicstaticvoidmain String args Leafx newLeaf x increment increment increment print 重載方法名 方法名相同參數不同參數的個數不同參數的類型不同參數的類型次序不同不包括返回值 classMethodOverloading voidreceive inti System out println Receiveoneintdata voidreceive inti intj System out println Receivetwointdata voidreceive doubled System out println Receiveonedoubledata voidreceive Strings System out println ReceiveaString publicclassMethodOverloadingTest publicstaticvoidmain String args MethodOverloadingm newMethodOverloading m receive 1 m receive 1 2 m receive 3 14 m receive hello publicclassOverloadingOrder staticvoidprint Strings inti System out println String s int i staticvoidprint inti Strings System out println int i String s publicstaticvoidmain String args print Stringfirst 11 print 99 Intfirst 對象的構造和初始化 初始化和初始化方法構造方法 classRock Rock ThisistheconstructorSystem out println CreatingRock publicclassSimpleConstructor publicstaticvoidmain String args for inti 0 i 10 i newRock 對象的構造和初始化 重載構造方法 classRock Rock inti System out println CreatingRocknumber i publicclassSimpleConstructor publicstaticvoidmain String args for inti 0 i 10 i newRock i 對象的構造和初始化 默認構造方法如果已經定義了一個構建器 無論是否有自變量 編譯程序都不會幫我們自動合成一個 finalize方法垃圾收集器 classPoint intx y Stringname apoint Point x 0 y 0 Point intx inty Stringname this x x this y y this name name intgetX returnx intgetY returny PointnewPoint Stringname PointnewP newPoint x y name returnnewP voidprint System out println name x x y y publicclassUsingObject publicstaticvoidmain String args Pointp newPoint p print Pointp1 newPoint 50 50 anewPoint p1 print System out println p1 x p1 y System out println p1 getX p1 getY p1 newPoint hello print newPoint 10 15 anothernewpoint print 子類 組合hasa繼承isaextends關鍵字protected單重繼承 classCleanser privateStrings newString Cleanser publicvoidappend Stringa s a publicvoiddilute append dilute publicvoidapply append apply publicvoidscrub append scrub publicvoidprint System out println s publicstaticvoidmain String args Cleanserx newCleanser x dilute x apply x scrub x print publicclassDetergentextendsCleanser publicvoidscrub append Detergent scrub super scrub publicvoidfoam append foam publicstaticvoidmain String args Detergentx newDetergent x dilute x apply x scrub x foam x print System out println Testingbaseclass Cleanser main args classArt Art System out println Artconstructor classDrawingextendsArt Drawing System out println Drawingconstructor publicclassCartoonextendsDrawing Cartoon System out println Cartoonconstructor publicstaticvoidmain String args Cartoonx newCartoon 多態(tài)性 向上映射instanceof運算符對象轉換Shapes java 覆蓋方法 繼承的過程繼承全部內容增加內容改變內容覆蓋方法一致的聲明 名稱 參數 不低于基類的存取權限 java包 分割系統(tǒng)包package包的命名原則導入import與include環(huán)境變量 Java語言程序設計 異常處理 異常 什么是異常正常情況之外的一種 異常 在問題發(fā)生的時候 我們可能不知具體該如何解決 但肯定知道已不能不顧一切地繼續(xù)下去 此時 必須堅決地停下來 并由某人 某地指出發(fā)生了什么事情 以及該采取何種對策 異常的分類 異常示例 publicclassHelloWorld publicstaticvoidmain Stringargs inti 0 Stringgreeting Helloworld No Imeanit HELLOWORD while i 4 System out println greeting i i 異常處理 try塊若位于一個方法內部 并 擲 出一個違例 或在這個方法內部調用的另一個方法產生了違例 那個方法就會在違例產生過程中退出 若不想離開方法 可在那個方法內部設置一個特殊的代碼塊 用它捕獲違例 這就叫作 try塊 因為要在這個地方 嘗試 各種方法調用 try塊屬于一種普通的作用域 用一個try關鍵字開頭 try 可能產生違例的代碼 異常處理 catch生成的違例必須在某個地方中止 這個 地方 便是違例控制器或者違例控制模塊 而且針對想捕獲的每種違例類型 都必須有一個相應的違例控制器 違例控制器緊接在try塊后面 且用catch 捕獲 關鍵字標記 如下所示 try Codethatmightgenerateexceptions catch Type1id1 HandleexceptionsofType1 catch Type2id2 HandleexceptionsofType2 catch Type3id3 HandleexceptionsofType3 異常處理 捕獲所有違例catch Exceptione 這段代碼能捕獲任何違例 所以在實際使用時最好將其置于控制器列表的末尾 防止跟隨在后面的任何特殊違例控制器失效 publicclassExceptionMethods publicstaticvoidmain String args try thrownewException Here smyException catch Exceptione System out println CaughtException System out println e getMessage e getMessage System out println e toString e toString System out println e printStackTrace e printStackTrace 異常處理 finally無論一個違例是否在try塊中發(fā)生 我們經常都想執(zhí)行一些特定的代碼 為達到這個目的 可在所有違例控制器的末尾使用一個finally從句 publicclassFinallyWorks staticintcount 0 publicstaticvoidmain String args while true try post incrementiszerofirsttime if count 0 thrownewException System out println Noexception catch Exceptione System out println Exceptionthrown finally System out println infinallyclause if count 2 break outof while 常見異常 ArithmetricExceptionNullPointerExceptionNegativeArraySizeExceptionArrayIndexOutOfBoundsExceptionSecurityExceptionIOException 拋出異常 異常的處理機制違例機制的一項好處就是能夠簡化錯誤控制代碼 我們再也不用檢查一個特定的錯誤 然后在程序的多處地方對其進行控制 此外 也不需要在方法調用的時候檢查錯誤 因為保證有人能捕獲這里的錯誤 我們只需要在一個地方處理問題 違例控制模塊 或者 違例控制器 這樣可有效減少代碼量 并將那些用于描述具體操作的代碼與專門糾正錯誤的代碼分隔開 拋出異常 創(chuàng)建自己的異常 為什么要創(chuàng)建異常創(chuàng)建異常代碼為創(chuàng)建自己的違例類 必須從一個現(xiàn)有的違例類型繼承 最好在含義上與新違例近似 產生異常thrownewmyExeption classMyExceptionextendsException publicMyException publicMyException Stringmsg super msg publicclassInheriting publicstaticvoidf throwsMyException System out println T
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 文化遺產數字化保護2025年與數字文化遺產保護技術合作模式研究報告
- 認知康復指導
- 新生兒護理敏感指標管理
- 養(yǎng)老社區(qū)智能化建設與社區(qū)居民需求匹配度研究報告
- 胸腔閉式引流水封瓶護理
- 牙齦癌的護理查房案例
- 無痛康復演講
- 呼吸機系統(tǒng)組成結構
- 突發(fā)性耳聾的治療及護理
- 電影中的心理疾病呈現(xiàn)與解析
- 老產品芯片1-gc2145d模組設計指南
- 廣東省中山市20222022學年下學期期末考試八年級英語試卷
- 油脂制取與加工工藝學
- 創(chuàng)新創(chuàng)業(yè)指導把握創(chuàng)業(yè)機會課件
- 部編版道德與法治五(下)第三單元百年追夢復興中華教學課件
- 第三章工程師的責任 工程倫理學課件
- 2022年湖南省普通高中學業(yè)水平考試語文試卷及參考答案
- 傳統(tǒng)節(jié)日端午節(jié)主題班會PPT模板
- 木材采購合同參考
- 1389國開電大本科《理工英語4》網上形考任務(單元自測1至8)試題及答案(精華版)
- 設備供貨投標實施方案
評論
0/150
提交評論