Ebook l­p tr¬nh Java h°»›ng ‘»‘i t°»£ng...

download Ebook l­p tr¬nh Java h°»›ng ‘»‘i t°»£ng (Ting vi»‡t)

of 193

  • date post

    07-Dec-2014
  • Category

    Documents

  • view

    439
  • download

    7

Embed Size (px)

description

Ebook lập trình Java hướng đối tượng (Tiếng việt)

Transcript of Ebook l­p tr¬nh Java h°»›ng ‘»‘i t°»£ng...

  • 1. B gio dc v o to Trng i hc s phm k thut hng yn ---------------ooo------------------
  • 2. cng bi ging Java c s Chng 1 CC KHI NIM C BN BI 1. LM QUEN VI JAVA I. Lch s java Java l mt ngn ng lp trnh c Sun Microsystems gii thiu vo thng 6 nm 1995. T , n tr thnh mt cng c lp trnh ca cc lp trnh vin chuyn nghip. Java c xy dng trn nn tng ca C v C++. Do vy n s dng cc c php ca C v cc c trng hng i tng ca C++. Ban u Java c thit k lm ngn ng vit chng trnh cho cc sn phm in t dn dng nh u video, tivi, in thoi, my nhn tin.. . Tuy nhin vi s mnh m ca Java khin n ni ting n mc vt ra ngoi s tng tng ca cc nh thit k ra n. Java khi thu tn l Oak- l cy si mc pha sau vn phng ca nh thit k chnh ng Jame Gosling, sau ny ng thy rng c ngn ng lp trnh tn Oak ri, do vy nhm thit k quyt nh i tn, Java l ci tn c chn, Java l tn ca mt qun cafe m nhm thit k java hay n ung. II. Java em l ai Java l ngn ng lp trnh hng i tng, do vy khng th dng Java vit mt chng trnh hng chc nng. Java c th gii quyt hu ht cc cng
  • 3. vic m cc ngn ng khc c th lm c. Java l ngn ng va bin dch va thng dch. u tin m ngun c bin dch bng cng c JAVAC chuyn thnh dng ByteCode. Sau c thc thi trn tng loi my c th nh chng trnh thng dch JAVA. Mc tiu ca cc nh thit k Java l cho php ngi lp trnh vit chng trnh mt ln nhng c th chy trn bt c phn cng c th, th nn khu hiu ca cc nh thit k Java l Write One, Run Any Where. Ngy nay, Java c s dng rng ri vit chng trnh chy trn Internet. N l ngn ng lp trnh hng i tng c lp thit b, khng ph thuc vo h iu hnh. Java khng ch dng vit cc ng dng chy n l hay trong mng m cn xy dng cc trnh iu khin thit b cho in thoi di ng, PDA, II. Mt s c trng ca java 1.n gin Nhng ngi thit k mong mun pht trin mt ngn ng d hc v quen thuc vi a s ngi lp trnh. Java ta nh C++, nhng lc b i cc c trng phc tp, khng cn thit ca C v C++ nh: thao tc con tr, thao tc nh ngha chng ton t (operator overloading), Java khng s dng lnh goto cng nh file header (.h). Cu trc struct v union cng c loi b khi Java. Nn c ngi bo Java l C++--, ng bo java l C++ nhng b i nhng th phc tp, khng cn thit. 2. Hng i tng C th ni java l ngn ng lp trnh hon ton hng i tng, tt cc trong java u l s vt, u u cng l s vt. 3. c lp vi h nn Mc tiu chnh ca cc nh thit k java l c lp vi h nn hay cn gi
  • 4. l c lp phn cng v h iu hnh. y l kh nng mt chng trnh c vit ti mt my nhng c th chy c bt k u Tnh c lp vi phn cng c hiu theo ngha mt chng trnh Java nu chy ng trn phn cng ca mt h my no th n cng chy ng trn tt c cc h my khc. Mt chng trnh ch chy ng trn mt s h my c th c gi l ph thuc vo phn cng. Tnh c lp vi h iu hnh c hiu theo ngha mt chng trnh Java c th chy c trn tt c cc h iu hnh. Mt chng trnh ch chy c trn mt s h iu hnh c gi l ph thuc vo h iu hnh. Cc chng trnh vit bng java c th chy trn hu ht cc h nn m khng cn phi thay i g, iu ny c nhng ngi lp trnh t cho n mt khu hiu vit mt ln, chy mi ni, iu ny l khng th c vi cc ngn ng lp trnh khc. i vi cc chng trnh vit bng C, C++ hoc mt ngn ng no khc, trnh bin dch s chuyn tp lnh thnh m my (machine code), hay lnh ca b vi x l. Nhng lnh ny ph thuc vo CPU hin ti trn my bn. Nn khi mun chy trn loi CPU khc, chng ta phi bin dch li chng trnh. 4. Mnh m Java l ngn ng yu cu cht ch v kiu d liu, vic p kiu t ng ba bi ca C, C++ nay c hn ch trong Java, iu ny lm chng trnh r rng, sng sa, t li hn.Java kim tra lc bin dch v c trong thi gian thng dch v vy Java loi b mt mt s loi li lp trnh nht nh.Java khng s dng con tr v cc php ton con tr. Java kim tra tt c cc truy nhp n mng, chui khi thc thi m bo rng cc truy nhp khng ra ngoi gii hn kch thc. Trong cc mi trng lp trnh truyn thng, lp trnh vin phi t mnh cp pht b nh. Trc khi chng trnh kt thc th phi t gii phng b nh cp. Vn ny sinh khi lp trnh vin qun gii phng b nh xin cp trc . Trong chng trnh Java, lp trnh vin khng phi bn tm n vic cp pht
  • 5. b nh. Qa trnh cp pht, gii phng c thc hin t ng, nh dch v thu nht nhng i tng khng cn s dng na (garbage collection). C ch by li ca Java gip n gin ha qa trnh x l li v hi phc sau li. 5. H tr lp trnh a tuyn y l tnh nng cho php vit mt chng trnh c nhiu on m lnh c chy song song vi nhau. Vi java ta c th vit cc chng trnh c kh nng chy song song mt cch d dng, hn th na vic ng b ti nguyn dng chung trong Java cng rt ng gin. iu ny l khng th c i vi mt s ngn ng lp trnh khc nh C/C++, pascal 6. Phn tn Java h tr y cc m hnh tnh ton phn tn: m hnh client/server, gi th tc t xa 7. H tr internet Mc tiu quan trng ca cc nh thit k java l to iu kin cho cc nh pht trin ng dng c th vit cc chng trnh ng dng internet v web mt cch d dng, vi java ta c th vit cc chng trnh s dng cc giao thc TCP, UDP mt cch d dng, v lp trnh web pha my khch java c cng ngh java applet, v lp trnh web pha my khch java c cng ngh servlet/JSP, v lp trnh phn tn java c cng ngh RMI, CORBA, EJB, Web Service. 8. Thng dch Cc chng trnh java cn c thng dch trc khi chy, mt chng trnh java c bin dch thnh m byte code m c lp vi h nn, chng trnh thng dch java s nh x m byte code ny ln mi nn c th, iu ny khin java chm chp i phn no.
  • 6. III. Cc kiu ng dng Java Vi Java ta c th xy dng cc kiu ng dng sau: 1. ng dng Applets Applet l chng trnh Java c to ra s dng trn Internet thng qua cc trnh duyt h tr Java nh IE hay Netscape. Applet c nhng bn trong trang Web. Khi trang Web hin th trong trnh duyt, Applet s c ti v v thc thi ti trnh duyt. 2. ng dng dng lnh (console) Cc chng trnh ny chy t du nhc lnh v khng s dng giao din ha. Cc thng tin nhp xut c th hin ti du nhc lnh. 3. ng dng ha y l cc chng trnh Java chy c lp cho php ngi dng tng tc qua giao din ha. 4. JSP/Servlet Java thch hp pht trin ng dng nhiu lp. Applet l chng trnh ha chy trn trnh duyt ti my trm. cc ng dng Web, my trm gi yu cu ti my ch. My ch x l v gi kt qu tr li my trm. Cc Java API chy trn my ch chu trch nhim x l ti my ch v tr li cc yu cu ca my trm. Cc Java API chy trn my ch ny m rng kh nng ca cc ng dng Java API chun. Cc ng dng trn my ch ny c gi l cc JSP/Servlet. hoc Applet ti my ch. X l Form ca HTML l cch s dng n gin nht ca JSP/Servlet. Chng cn c th c dng x l d liu, thc thi cc giao dch v thng c thc thi thng qua my ch Web. 5. ng dng c s d liu
  • 7. Cc ng dng ny s dng JDBC API kt ni ti c s d liu. Chng c th l Applet hay ng dng, nhng Applet b gii hn bi tnh bo mt. 6. ng dng mng Java l mt ngn ng rt thch hp cho vic xy dng cc ng dng mng. Vi th vin Socket bn c th lp trnh vi hai giao thc: UDP v TCP. 7. ng dng nhiu tng Vi Java bn c th xy dng phn tn nhiu tng vi nhiu h tr khc nhau nh: RMI, CORBA, EJB, Web Service 8. ng dng cho cc thit b di ng Hin nay phn ln cc thit b di ng nh: in thoi di ng, my tr gip c nhn u h tr Java. Th nn bn c th xy dng cc ng dng chy trn cc thit b di ng ny. y l mt kiu ng dng kh hp dn, bi v cc thit b di ng ny ngy cng ph bin v nhu cu c cc ng dng chy trn , c bit l cc ng dng mang tnh cht gii tr nh game IV. My o Java (JVM-Java Virtual Machine) My o l mt phn mm m phng mt my tnh tht (my tnh o). N c tp hp cc lnh logic xc nh cc hot ng ca my tnh v c mt h iu hnh o. Ngi ta c th xem n nh mt my tnh tht (my tnh c phn cng o, h iu hnh o). N thit lp cc lp tru tng cho: Phn cng bn di, h iu hnh, m bin dch. Trnh bin dch chuyn m ngun thnh tp cc lnh ca my o m khng ph thuc vo phn cng v h iu hnh c th. Trnh thng dch trn mi my s chuyn tp lnh ny thnh chng trnh thc thi. My o to ra mt mi trng bn trong thc thi cc lnh bng cch: 1 Np cc file .class
  • 8. 2 Qun l b nh 3 Dn rc Vic khng nht qun ca phn cng lm cho my o phi s dng ngn xp lu tr cc thng tin sau: 1 Cc Frame cha cc trng thi ca cc phng thc. 2 Cc ton hng ca m bytecode. 3 Cc tham s truyn cho phng thc. 4 Cc bin cc b. Khi JVM thc thi m, mt thanh ghi cc b c tn Program Counter c s dng. Thanh ghi ny tr ti lnh ang thc hin. Khi cn thit, c th thay i ni dung thanh ghi i hng thc thi ca chng trnh. Trong trng hp thng thng th tng lnh mt ni tip nhau s c thc thi. Mt khi nim thng dng khc trong Java l trnh bin dch Just In Time-JIT. Cc trnh duyt thng dng nh Netscape hay IE u c JIT bn trong tng tc thc thi chng trnh Java. Mc ch chnh ca JIT l chuyn tp lnh bytecode thnh m my c th cho tng loi CPU. Cc lnh ny s c lu tr v s dng mi khi gi n.
  • 9. BI 2 NN TNG CA JAVA I. Tp k t dng trong java Mi ngn ng ni chung, ngn ng lp trnh ni ring u phi xy dng trn mt tp hp ch ci (hay cn gi l bng ch ci), cc k t c nhm li theo mt cch no to thnh cc t, cc t li c nhm li thnh cc cu (trong ngn ng lp trnh gi l cu lnh), mt chng trnh my tnh tnh l mt tp cc cu lnh c b tr theo mt trt t m ngi vit ra chng sp t Ngn ng java c c xy dng trn bng ch ci unicode, do vy ta c th dng cc k t unicode t tn cho cc nh danh. II. T kho ca Java Mi ngn ng lp trnh c mt tp cc t kho, ngi lp trnh phi s dng t kho theo ng ngha m ngi thit k ngn ng ra, ta khng th nh ngha li ngha ca cc t kho, nh s dng n t tn bin, hm.. Sau y l mt s t kho thng gp: T kha M t abstract S dng khai bo lp, phng thc tru tng boolean Kiu d liu logic break c s dng kt thc vng lp hoc cu trc switch byte kiu d liu s nguyn case c s dng tr