Java語言與面向?qū)ο笏枷敕g(中英文)_第1頁
Java語言與面向?qū)ο笏枷敕g(中英文)_第2頁
Java語言與面向?qū)ο笏枷敕g(中英文)_第3頁
Java語言與面向?qū)ο笏枷敕g(中英文)_第4頁
Java語言與面向?qū)ο笏枷敕g(中英文)_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、java語言與面向?qū)ο笏枷雑ava,是由sunmicrosystems公司于1995年5月推出的java程序設(shè)計語言和java的總稱。用java實現(xiàn)的hotjava瀏覽器(支持javaapplet)顯示了java的魅力:跨平臺、動態(tài)的web、internet計算。從此,java被廣泛接受并推動了web的迅速發(fā)展,常用的瀏覽器現(xiàn)在均支持javaapplet。1java語言的特點java語言適用于internet環(huán)境,是一種被廣泛使用的網(wǎng)絡(luò)編程語言,它具有如下的一些特點:簡單java語言的語法規(guī)則和c+類似,但java語言取消了指針和多重繼承,統(tǒng)一使用引用來指示對象(c+中有兩種形式,實際上是兩種

2、產(chǎn)生對象的途徑,而java中只有一種),通過自動垃圾收集免去了程序設(shè)計人員對于內(nèi)存塊的釋放工作。面向?qū)ο螅ń谕耆﹋ava語言為了提高效率,定義了幾個基本的數(shù)據(jù)類型以非類的方式實現(xiàn),余下的所有數(shù)據(jù)類型都以類的形式進行封裝,程序系統(tǒng)的構(gòu)成單位也是類。因而幾乎可以認為是完全面向?qū)ο?。平臺無關(guān)性(可移植、跨平臺)java虛擬機(jvm)是在各種體系結(jié)構(gòu)真實機器中用軟件模擬實現(xiàn)的一種想象機器,必要時候可以用硬件實現(xiàn)。當然,這些虛擬機內(nèi)部實現(xiàn)各異,但其功能是一致的執(zhí)行統(tǒng)一的java虛擬機指令。java編譯器將java應(yīng)用程序的源代碼文件(.java)翻譯成java字節(jié)碼文件(.class),它是由ja

3、va虛擬機指令構(gòu)成的。由于是虛擬機器,因而java虛擬機執(zhí)行java程序的過程一般稱為解釋。依賴于虛擬機技術(shù),java語言具有與機器體系結(jié)構(gòu)無關(guān)的特性,即java程序一旦編寫好之后,不需進行修改就可以移植到任何一臺體系結(jié)構(gòu)不同的機器上。從操作系統(tǒng)的角度看,執(zhí)行一次java程序的過程就是執(zhí)行一次java虛擬機進程的過程。面向網(wǎng)絡(luò)編程java語言產(chǎn)生之初就面向網(wǎng)絡(luò),在jdk中包括了支持tcp/ip、http和ftp等協(xié)議的類庫。多線程支持多線程是程序同時執(zhí)行多個任務(wù)的一種功能。多線程機制能夠使應(yīng)用程序并行執(zhí)行多項任務(wù),其同步機制保證了各線程對共享數(shù)據(jù)的正確操作。良好的代碼安全性運行時(runtim

4、e)一詞強調(diào)以動態(tài)的角度看程序,研究程序運行時候的動態(tài)變化,也用運行時環(huán)境一詞表達類似的含義。java技術(shù)的很多工作是在運行時完成的,如加強代碼安全性的校驗操作。一般地,java技術(shù)的運行環(huán)境執(zhí)行如下三大任務(wù):l加載代碼由類加載器執(zhí)行類加載器為程序的執(zhí)行加載所需要的全部類(盡可能而未必同時)。l校驗代碼由字節(jié)碼校驗器執(zhí)行java代碼在實際運行之前要經(jīng)過幾次測試。字節(jié)碼校驗器對程序代碼進行四遍校驗,這可以保證代碼符合jvm規(guī)范并且不破壞系統(tǒng)的完整性。如檢查偽造指針、違反對象訪問權(quán)限或試圖改變對象類型的非法代碼。l執(zhí)行代碼由運行時的解釋器執(zhí)行自動垃圾收集許多編程語言都允許在程序運行時動態(tài)分配內(nèi)存塊

5、,分配內(nèi)存塊的過程由于語言句法不同而有所變化,但總是要返回指向存儲區(qū)起始位置的指針。在c,c+及其它一些語言中,程序員負責取消分配內(nèi)存塊。有時這是一件很困難的事情。因為程序員并不總是事先知道內(nèi)存塊應(yīng)在何時被釋放。當在系統(tǒng)中沒有能夠被分配的內(nèi)存塊時,可導致程序癱瘓,這種程序被稱作具有內(nèi)存漏洞。當分配內(nèi)存塊不再需要時,程序或運行環(huán)境應(yīng)取消分配內(nèi)存塊。l垃圾收集就是將不再需要的已分配內(nèi)存塊回收。l在其它一般的語言中,取消分配是程序員的責任。ljava編程語言提供了一種系統(tǒng)級線程以跟蹤存儲區(qū)分配,來完成垃圾收集:l可檢查和釋放不再需要的存儲塊l可自動完成上述工作l可在jvm實現(xiàn)周期中,產(chǎn)生意想不到的變

6、化l良好的代碼健壯性java能夠檢查程序在編譯和運行時的錯誤。類型檢查能幫助用戶檢查出許多在開發(fā)早期出現(xiàn)的錯誤。同時很多集成開發(fā)工具(ide)的出現(xiàn)使編譯和運行java程序更加容易,并且很多集成開發(fā)工具(如eclipse)都是免費的。2面向?qū)ο笈c面向過程的差異一種程序設(shè)計語言的產(chǎn)生,不僅是程序設(shè)計技術(shù)的改進,也包含了表達和認知思想的進步。以c語言為代表的部分早期語言,被稱為面向過程的語言,不僅因為其程序設(shè)計的表達形式是以過程為基本元素,本質(zhì)上更在于此時對計算機化的系統(tǒng)的理解的主導思想還是控制流或者數(shù)據(jù)流的,構(gòu)成系統(tǒng)的要素是模塊處理邏輯。面向?qū)ο笳Z言的產(chǎn)生,是因為對于系統(tǒng)的理解或抽象到了更為高級

7、的層次。此時的認知思想不僅更接近于現(xiàn)實世界,其抽象程度也很高。因而,既有易懂的一方面,也有難懂的另一方面,就看理解的境界了。2.1面向過程思想回顧面向過程思想和程序設(shè)計語言的體現(xiàn),可以簡單總結(jié)為以下幾條:對系統(tǒng)的認識采用結(jié)構(gòu)化分析的過程自頂向下,逐步求精地進行功能分解;每個功能就是處理數(shù)據(jù)的一個模塊,因而ipo描述充分刻畫了模塊的內(nèi)外特征;每個模塊都是控制流的,因而有典型的三種程序結(jié)構(gòu)順序、分支、循環(huán);模塊間的關(guān)系通過調(diào)用維系,因而模塊成為了函數(shù),這也是面向過程方式下最主要的代碼重用方式;盡管函數(shù)調(diào)用有參數(shù)和返回值,但數(shù)據(jù)總是和函數(shù)分開的,不僅有了著名的斷言程序=算法+數(shù)據(jù),也使得絕對平等產(chǎn)生

8、了無序的麻煩所有的函數(shù)和數(shù)據(jù)的關(guān)聯(lián)關(guān)系沒有限制;如果要使用數(shù)據(jù)流,那么只能是在數(shù)據(jù)產(chǎn)生之后的適當時機調(diào)用函數(shù)。這一時期的主要缺點是:對系統(tǒng)的理解比較難,系統(tǒng)地開發(fā)效率低,代碼的可重用性差。2.2面向?qū)ο笏枷虢榻B面向?qū)ο笏枷?,對現(xiàn)實世界采用直觀的理解,計算機化時候采用深度的抽象,簡單地可以總結(jié)如下:系統(tǒng)是由事物構(gòu)成的,事物之間是有聯(lián)系的,復(fù)雜的事物也是系統(tǒng);系統(tǒng)與系統(tǒng)、系統(tǒng)與事物、事物與事物之間是有明確界限(邊界)的;系統(tǒng)或事物的狀態(tài)刻畫可以用屬性表示,屬性一般是些簡單的數(shù)據(jù),如果復(fù)雜那就是事物了;系統(tǒng)或事物的狀態(tài)會發(fā)生變化,稱為行為,產(chǎn)生變化是有原因的(內(nèi)部的或外部的),變化的過程可能是復(fù)雜的

9、;不同的事物之間會具有共同的屬性和行為,共同的極端情形就是完全包含。基于以上的認識,一個運行時(動態(tài))的具體系統(tǒng)或事物,是由幾個更小的具體事物構(gòu)成(極端的事物就是一個簡單的屬性數(shù)據(jù)),它們是不斷發(fā)生變化的。如果對事物這一概念進行了有效的抽象,那么問題就迎刃而解。首先,將任何一個具體的事物稱為對象(object),它的極端情形就是過去的變量;事物是分類的,每一類事物都具有統(tǒng)一的屬性和行為,即類型抽象數(shù)據(jù)類型,簡稱為類(class);行為既然是過程,那么就抽象成函數(shù),命名為方法,以示區(qū)別。例如:描述身高或姓名,各自只是一個簡單的數(shù)據(jù)變量;描述一個學生,可以使用學號、姓名、宿舍、班級等;那更換宿舍算

10、什么呢!那描述宿舍、班級,又要有許多個項目。3面向?qū)ο蟪绦蛟O(shè)計中的主要概念和特征面向?qū)ο蟪绦蛟O(shè)計(objectorientedprogramming,oop)語言中,為了進行更為高度的抽象,會引入一些現(xiàn)實世界中難于找到的概念,但對于一個程序語言來說確實很有價值的。以下暫時介紹的概念基本上都來源于對現(xiàn)實世界的抽象,要從程序設(shè)計的角色中去理解它們。面向?qū)ο蟪绦蛟O(shè)計使系統(tǒng)更易于理解,也使代碼具有更好的重用性、可擴展性、易于管理和維護。3.1主要概念(1)、類類是對一類事物的抽象表示,其角色就相當于數(shù)據(jù)類型,當然可以算作復(fù)雜的數(shù)據(jù)類型。如學生、宿舍、班級。(2)、對象對象表示一個具體的事物,其角色就是

11、變量,即一個復(fù)雜數(shù)據(jù)類型xx類的變量。如周瑜、張飛、瑜飛居,飛虎班。(3)、成員一個事物的構(gòu)成元素,討論類的時候就是虛擬的,討論對象的時候就是具體的。如在類中,一個變量屬于成員成員變量,一個對象也屬于成員成員對象,一個函數(shù)也屬于成員成員方法。3.2主要特征oop語言有三個特征:封裝、繼承及多態(tài)性。(1)、封裝類的構(gòu)成包括成員變量/對象與成員方法,這樣將相關(guān)的數(shù)據(jù)與函數(shù)包裝在一起,同其他的類相區(qū)分,就是封裝。顯然,避免了面向過程語言的平行缺陷,說明了類和成員之間的所屬關(guān)系。進一步地,可以限制類的成員在外部的可見性,那么就將封裝體現(xiàn)得更完美。(2)、繼承當一種事物甲完全是另一種事物乙的特例,那么,

12、一般地,類甲只是比類乙多出一些成員變量/對象與成員方法,稱為類甲繼承類乙,類甲稱為(類乙的)子類,類乙稱為(類甲的)父類。父類也稱為基類、超類,子類也稱為導出類、派生類。顯然編寫子類就沒有必要重復(fù)書寫父類中乙有的代碼部分,這是oop中最典型的代碼重用。(3)、多態(tài)多態(tài)表示一個類的某種行為存在多種實現(xiàn)版本。簡單的情況是在一個類中,給出多種不同的實現(xiàn),復(fù)雜的情況是在多個子類中各自給出不同的實現(xiàn)。overviewofjavalanguagejava,fromsunmicrosystems,launchedinmay1995ofthejavaprogramminglanguageandjavainge

13、neral.hotjavaimplementationwithjavabrowser(supportingjavaapplet)showsthecharmofjava:cross-platform,dynamicweb,internetcomputing.sincethen,javaiswidelyacceptedandpromotedtherapiddevelopmentofweb,popularbrowsersnowsupportjavaapplet1javalanguagefeaturesjavalanguageforinternetenvironment,isawidelyusedwe

14、bprogramminglanguage,ithasthefollowingfeatures:simplejavalanguageandc+syntaxrulesaresimilar,butcanceledthejavalanguageandmultipleinheritancepointer,unifiedbyreferencetoindicatetheobject(c+intwoforms,isactuallytwowaystogenerateobjects,andjava,thereisonlyone)byautomaticgarbagecollectioneliminatesthene

15、edforprogrammersworkingforthereleaseofthememoryblock.object-oriented(nearlycomplete)javalanguageinordertoimproveefficiency,definedafewbasicdatatypestoachieveanon-classmanner,andtheremainingclassofalldatatypesintheformofpackaging,theprogramstructureofthesystemunitisalsoaclass.thereforeconsideredtobea

16、lmostcompletelyobject-oriented.platformindependent(portable,cross-platform)javavirtualmachine(jvm)istrueinavarietyofmachinearchitecturesimplementedusingsoftwaretosimulateanimaginarymachine,whennecessary,canbeimplementedbyhardware.ofcourse,theinternalimplementationofthesevirtualmachinesvary,butitsfun

17、ctionisthesame-theimplementationofaunifiedjavavirtualmachineinstructions.javacompilerjavaapplicationssourcecodefiles(.java)translatedintojavabytecodefile(.class),whichiscomposedofjavavirtualmachineinstructions.becauseitisavirtualmachine,andthustheimplementationofjavavirtualmachinejavaprogramscommonl

18、yreferredtoastheprocessofinterpretation.reliesonvirtualmachinetechnology,javalanguagehasnothingtodowiththecharacteristicsofthemachinearchitecture,thatjavaprograms,onceafterbeingwritten,canbetransplantedwithoutmodificationtoanyoneonadifferentmachinearchitecture.fromtheoperatingsystempointofview,thepr

19、ocessofimplementationofajavaprogramistheprocessofimplementationofajavavirtualmachineprocess.orientednetworkprogrammingjavalanguageforgeneratingthebeginningofthenetworkofsupportinthejdkincludestcp/ip,httpandftpprotocolssuchasclasslibraries.multi-threadingsupportmultithreadingistosimultaneouslyperform

20、multipletasksofafunction.multi-threadingmechanismthatenablesapplicationstoperformmultipletasksinparallel,thevariousthreadsynchronizationmechanismstoensureproperoperationoftheshareddata.goodsecuritycoderuntime(runtime)termperspectiveemphasizesthedynamicprocessofthedynamicchangeswhentheprogramisrunnin

21、g,butalsowiththeexpressionoftheruntimeenvironmentsimilartothemeaningoftheword.javatechnologyisalotofworkdoneatruntime,suchasstrengtheningsecuritycodeverificationoperations.ingeneral,javatechnologyruntimeenvironmentperformsthefollowingthreemajortasks:lloadingcode-theimplementationbytheclassloaderclas

22、sloaderfortheimplementationoftheproceduresrequiredtoloadallclasses(asfaraspossibleandnotnecessarilythesame).lvalidationcode-theimplementationofthebytecodeverifierjavacodebeforetheactualoperationafterseveraltests.bytecodeverifierforfourtimesonthevalidationcode,whichcanensurecodecomplianceanddoesnotde

23、stroythejvmspecificationsystemintegrity.if-checkforgedpointers,objectaccessrightsviolationorattempttochangetheobjecttypeofillegalcode.lcodeexecution-fromtheimplementationoftheruntimeinterpreterlautomaticgarbagecollectionmanyprogramminglanguagesallowprogramsrunninginthememoryblockdynamicallyallocated

24、,theprocessofallocatingmemoryblocksbecauseofthelanguagesyntaxvaries,butalwayspointtothestoretoreturntothestartingpositionofthepointer.inc,c+andotherlanguages,theprogrammerisresponsiblefordeallocationofmemoryblocks.sometimesthisisaverydifficulttask.becauseprogrammersdonotalwaysknowinadvancewhenablock

25、ofmemoryshouldbereleased.whennotinthesystemcanbeallocatedmemoryblock,itcancausetheprogramtoastandstill,thisprocedureiscalledamemoryleak.whentheallocatedmemoryblockisnolongerneeded,theprogramshouldbecanceledortheoperatingenvironmenttoallocatememoryblock.lgarbagecollectionisnolongerneededrecoveryofall

26、ocatedmemoryblock.linotherlanguagesingeneral,istheprogrammersresponsibilitytocanceltheallocation.ljavaprogramminglanguageprovidesasystem-levelthreadstokeeptrackofmemoryallocation,tocompletethegarbagecollection:nnolongerneedtocheckandreleasethememoryblock,ntheaboveworkcanbecompletedautomatically,nint

27、hejvmimplementationcycle,resultinginunexpectedchanges,lgoodrobustnessofthecode.javaprogramtocheckatcompileandrun-timeerrors.typeofexaminationcanhelpdetectmanyerrorsearlyindevelopment.meanwhile,manyintegrateddevelopmenttools(ide)tocompileandruntheemergenceofjavaprogramseasierandalotofintegrateddevelo

28、pmenttools(egeclipse)arefree.2object-orientedandprocess-orienteddifferencestheproductionofaprogramminglanguage,programmingisnotonlyatechnicalimprovement,butalsoincludestheexpressionofideasandcognitiveprogress.theclanguageastherepresentativeoftheearlypartofthelanguage,knownasprocess-orientedlanguage,

29、notonlybecauseoftheirexpressionofprogramdesignisbasedonthebasicelementsoftheprocess,inessence,isnowmoreunderstandingofthecomputerizedsystemthedominantideologyorcontrolflowordataflow,constituteelementsofthesystemisthemodule-processinglogic.thegenerationofobject-orientedlanguages,becausetheunderstandi

30、ngofthesystemortheabstracttoamoreadvancedlevel.cognitivethinkingatthistimenotonlyclosertotherealworld,anditshighdegreeofabstraction.thus,botheasytounderstandontheonehand,therearealsodifficulttounderstandtheotherhand,toseetherealmofunderstanding.2.1reviewprocess-orientedthinkingprocess-orientedprogra

31、mminglanguage,thoughtandexpression,canbesimplysummarizedasthefollowingfew:understandingofthesystemstructureanalysisoftheprocess-top-down,stepwiserefinementtothefunctionaldecomposition;eachfunctionisadataprocessingmodule,whichfullydescribetheipodescribedtheinternalandexternalfeaturesofthemodule;eachm

32、oduleiscontrolflow,andthereforehaveatypicalstructureofthethreeprocedures-sequence,branch,loop;therelationshipbetweenthemodulebycallingthepreserve,whichhasbecomeafunctionmodule,whichisthemostimportantprocess-orientedwayofcodereuseunderway;althoughthefunctioncallparametersandreturnvalues,butthedataand

33、functionsarealwaysseparate,notonlywiththefamousassertion-program=algorithm+data,butalsomakestheabsoluteequalityofthedisorderhavetrouble-allthefunctionsanddatarelationshipisnolimit;ifyouwanttousethedataflow,thenthedatacanonlybeproducedinanappropriatetimeafterthecallfunction.themaindrawbackofthisperio

34、dare:understandingthesystemmoredifficult,thesystematicdevelopmentoflowefficiency,codereusabilityispoor.2.2oopobject-orientedthinkingontheuseofanintuitiveunderstandingoftherealworld,whentheuseofcomputerizeddepthofabstractioncanbesummarizedbrieflyasfollows:systemiscomposedofthings,thereisaconnectionbe

35、tweenthings,thecomplexityissystem;systemandthesystem,andthings,thingsandthingsisaclearboundarybetweenthe(boundary)of;systemorthestateofthingscandescribetheproperty,saidpropertyisusuallysomesimpledata,ifthatissomethingthecomplex;systemorthestateofthingswillchange,calledthebehaviorofadifferencethereis

36、areason(internalorexternal),changesintheprocessmaybecomplex;willhavedifferentthingsincommonbetweenthepropertiesandbehavior,acommonextremecaseiscompletelycontained.basedontheaboveunderstanding,aruntime(dynamic)tothespecificsystemorsomething,byseveralsmallerthingsthatconstituteaspecific(extremethingsi

37、sasimpleattributedata),whichisconstantlychanging.ifthisconceptofthingsaneffectiveabstract,thentheproblemissolved.firstofall,anyonespecificthingcalledtheobject(object),itistheextremecaseofpreviousvariables;thingsareclassified,eachclassofthingshavethepropertiesandbehaviorofuniform,thattype-abstractdat

38、atypes,referredtoasfortheclass(class);behaviorsinceitistheprocess,thenafunctionoftheabstract,namedforthemethodtoshowthedifference.forexample:describetheheightorname,eachjustasimpledatavariable;describeastudent,youcanusethestudentnumber,name,dorm,class,etc.;whatkindofaccommodationthatreplacedit!itdes

39、cribesthedorm,class,andalsohasmanyprojects.3object-orientedprogrammingconceptsandfeaturesofthemainobject-orientedprogramming(objectorientedprogramming,oop)language,inordertobemorehighlyabstract,willintroducesomeoftherealworld,theconceptisdifficulttofind,butforaprogramminglanguageisindeedvaluable.the

40、followingdescribestheconceptofbeingbasicallyderivedfromtheabstracttotherealworld,fromtheroleofprogramdesigntounderstandthem.object-orientedprogrammingtomakethesystemeasiertounderstand,butalsomakethecodebetterreusability,scalability,easymanagementandmaintenance.3.1mainconcepts(1) classclassistheabstr

41、actofaclassofthingsthatthedatatypeisequivalenttoitsrole,ofcourse,canberegardedascomplexdatatypes.suchasstudents,dormitories,class.(2) theobjectobjectrepresentsaspecificthing,itsroleisvariable,thatis,acomplexdatatype-xxclassvariables.suchaszhouyu,zhangfei,yuflyhome,tigerclasses.(3) memberstheelementsofathingtodiscusswhenthecl

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論