java基礎(chǔ)培訓(xùn)類封裝就是將成員變量私有化讓外界程序不能隨意改變_第1頁
java基礎(chǔ)培訓(xùn)類封裝就是將成員變量私有化讓外界程序不能隨意改變_第2頁
java基礎(chǔ)培訓(xùn)類封裝就是將成員變量私有化讓外界程序不能隨意改變_第3頁
java基礎(chǔ)培訓(xùn)類封裝就是將成員變量私有化讓外界程序不能隨意改變_第4頁
java基礎(chǔ)培訓(xùn)類封裝就是將成員變量私有化讓外界程序不能隨意改變_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

類的封裝就是將成員變量,讓外界程序不能隨意改變的類的成員變對(duì)這個(gè)變量進(jìn)行。對(duì)一個(gè)變量的操作,一般都有和賦值操作,我們一般定義兩個(gè)方法來實(shí)現(xiàn)這兩種操作,即:getXxx()setXxx();(注:這里需要理解,類的成員變量、屬性public *privateString*privateint通過getpublicStringgetName()return}通過setpublicvoidsetName(Stringname)=}publicintgetAge()return}publicvoidsetAge(intage)this.age=}}Java(可以實(shí)現(xiàn)多個(gè)接口),意味著一個(gè)類只能從另一個(gè)類繼承(被繼承的類叫做父類【基類,baseclass】,繼承的類叫做子類),Javaextends生成子類對(duì)象順序,JavapublicclassTestpublicstaticvoidmain(String[]args){Sonson=newSon();Sonson1=new}}classFatherpublicStringpublicFather()}publicFather(Stringname)=}}classSonextendsFatherpublicSon()}publicSon(Stringname)}}3).superpublicSon(Stringname)/}

(調(diào)用父類的構(gòu)造函數(shù))示調(diào)用父類的構(gòu)造函數(shù),此時(shí)super指的是對(duì)父類的的。在子類構(gòu)造函數(shù)中,super(Overloadthis()的方式調(diào)用,this()括號(hào)中的參數(shù)表示目標(biāo)構(gòu)造方法的參數(shù)。this()classSonextendsFatherpublicSon()}publicSon(Stringname)}@paramagepublicvoidsay o(intage){ oyourageis"+age);}@paramname@paramagepublicvoidsay o(Stringname,intage){System.out.println("hi"+name+"!yourageis"+age);}@paramage@paramnamepublicvoidsay o(intage,Stringname){System.out.println("hi"+name+"!yourageis"+age);}}(OverrideclassFatherpublicStringsay(Stringstr)return"yoursay"+}}classSonextendsFather*publicStringsay(Stringstr)return"yoursay}}請(qǐng)重載這個(gè)方法。意思,父類的指向子類,就是多態(tài)。Busbus=newCar();//父類的指向子類轎bus.run();//runrun(upcast(downcastCollectionCollectionCollectionObject,即(Elements。一些JavaSDKCollectionListSet。Collection,Collection有相同的元素。后一個(gè)構(gòu)造函數(shù)允許用戶一個(gè)CollectionListListJavalistItor()方法,返回一個(gè)ListItor接口,和標(biāo)準(zhǔn)的ItorListLinkedList,ArrayList,VectorStack。ArrayListnull,以及重復(fù)元素。ArrayListsize,isEmpty,get,setaddnO(n)的時(shí)間,其他的方法運(yùn)行時(shí)間為線性。ArrayList每個(gè)ArrayList實(shí)例都有一個(gè)容量(Capacity,即用于元素的數(shù)組的當(dāng)需要插入大量元素時(shí),在插入前可以調(diào)用ensureCapacity方法來增加ArrayListpublicclassTest1publicstaticvoidmain(String[]args){ArrayListlist=newArrayList();Listlist=Collections.synchronizedList(new;//}}LinkedListgetremoveinsertLinkedList的首部或尾部。這些操作使(stack(dequepeek、offer、pushArrayListLinkedList2).對(duì)于隨機(jī)get和set,ArrayList覺得優(yōu)于LinkedList,因?yàn)長inkedList3).對(duì)于新增和刪除操作add和remove,LinedList比較占優(yōu)勢(shì),因?yàn)锳rrayList的速度反而優(yōu)于LinkedList。但若是批量隨機(jī)的插入刪除數(shù)據(jù),ArrayListVector目前數(shù)組長度的100%,而arraylist50%.如過在集合中vector存在,A,A機(jī)制存在,A,ASetSetCollection,e1e2CollectionObjectObject.equals(Object)=true不僅實(shí)現(xiàn)了哈希算法,而且實(shí)現(xiàn)了鏈表數(shù)據(jù)結(jié)構(gòu)。TreeSetSortedSetMapMapkey,keyvalue。Mapkey-valueHashTableHashtableMapkey-valueget(key),這兩個(gè)基本操作的時(shí)間開銷為常數(shù)。getputpublicclassMapTestpublicstaticvoidmain(String[]args){Hashtablemap=newHashtable();map.put("1","o");map.put("2",Hashtablemap1=newHashtable(1);map1.put("1","o");map1.put("2","word");}}HashMapHashMapHashtableHashMap許nullnullvaluenullkeyHashMapCollection(values()CollectionloadfactorpublicclassMapTestpublicstaticvoidmain(String[]args){HashMapmap=newHashMap();map.put("1","o");map.put("2","word");map.put(null,null);}}HashMapTreeMapTreeMap(HashMap)。hashCode()equals()TreeMapHashTableHashMapHashMapMapHashMapkeyvalue三、I/OFile一個(gè)File類的對(duì)象,表示了磁盤上的文件或 FileFile類沒有指定信息怎樣從文件或向文件File File類還可以瀏覽 層次結(jié)構(gòu)publicclassFileTestpublicstaticvoidmain(String[]args)Filefile=newSystem.out.println("是否可執(zhí)行:System.out.println("是否可讀:System.out.println("是否可寫:System.out.println("文件名稱:System.out.println("文件絕對(duì)路徑://創(chuàng)建文件 //file.mkdir();file.createNewFile();}}練習(xí):在 I/OJava程序通過流來完成輸入/輸出。流是生產(chǎn)或消費(fèi)信息的抽象。流通過Java的輸入/輸出系統(tǒng)與物理設(shè)備。盡管與它們的物理設(shè)備不盡相同,網(wǎng)絡(luò)的不同。Javajava.io(以字符為處理單位或稱面向字符ReaderWriter。16讀

intread():一個(gè)字節(jié)數(shù)據(jù),并返回讀到的數(shù)據(jù),如果回-1,intread(byte[]b):將數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組,同時(shí)返回實(shí)際的intread(byte[]b,intoff,intlen):將數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組,同時(shí)返回實(shí)際的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置;len指定的最大字節(jié)數(shù)。intavailable():返回在不發(fā)生阻塞的情況下,可的字節(jié)數(shù)voidclose():publicclassFileTestpublicstaticvoidmain(String[]args)Filefile=new//定義輸入流對(duì)象InputStreamisnull;try{is=newbyte[]temp=newwhile(-1!=is.read(temp)){System.out.println(newString(temp,"gbk"));}}catch(FileNotFoundExceptione){}catch(IOExceptione)}finallyif(null!=is)try}catch(IOExceptione){}}}}}

voidwrite(intb)voidwrite(byte[]b)bvoidwrite(byte[]b,intoff,intlen)bofflenvoidflush()voidclose():publicclassFileTest1publicstaticvoidmain(String[]args)Filefile=newif(!file.exists())try}catch(IOExceptione){}}OutputStreamos=tryos=newStringstr="我要向文件中寫入內(nèi)容,嘻 寫寫}catch(FileNotFoundExceptione){}catch(IOExceptione){}finallyif(null!=os)try}catch(IOExceptione){}}}}}FileInputStream節(jié)點(diǎn)流,用于從文件中或往文件中寫入字節(jié)流。如果在構(gòu)FileOutputStreamBufferedInputStreamDataInputStreamJavaObjectInputStreamJavaPipedInputStreamPipedInputStreamI/O1、使用帶緩沖的流,實(shí)現(xiàn)上面實(shí)例中對(duì)文件的、寫入2、創(chuàng)建一 類有屬性實(shí)例化類將對(duì)象持久到 中,然后文件,將信息打印到控制臺(tái)中(提示:使用ObjectOutputStream流ReaderReaderread(),來實(shí)現(xiàn)對(duì)相應(yīng)外設(shè)的輸入操作。InputStreanpublicclassFileTestpublicstaticvoidmain(String[]args)Filefile=new//定義輸入流對(duì)象Readerreadernull;try{reader=newchar[]temp=newwhile(-1!=reader.read(temp))}}catch(FileNotFoundExceptione){}catch(IOExceptione)}finallyif(null!=reader)try}catch(IOExceptione){}}}}}WriterWriter中包含一套字符輸出流需要的方法,可以完成最基本的輸出數(shù)據(jù)到Writerwrite(),來實(shí)現(xiàn)對(duì)相應(yīng)外設(shè)的輸出操OutputStreampublicclassFileTest1publicstaticvoidmain(String[]args)Filefile=newif(!file.exists())try}catch(IOExceptione){}}Writeros=tryos=newStringstr="我要向文件中寫入內(nèi)容,嘻 寫寫}catch(FileNotFoundExceptione){}catch(IOExceptione){}finallyif(null!=os)try}catch(IOExceptione){}}}}}InputStreamReaderFileReaderInputStreamReader。FileWriter創(chuàng)建一個(gè)可以寫文件的Writer類。FileWriter繼承于OutputStreamWriterCharArrayReaderCharArrayReaderCharArrayWriterBufferedReaderBufferedWriterBufferedReaderBufferedWriterflush(Writer.flush(以用來確保數(shù)據(jù)緩沖區(qū)確實(shí)被寫到實(shí)際的輸出流。BufferedWriter可以通publicclassFileTest@param@throwspublicstaticvoidmain(String[]args)throwsIOException//TODOAuto-generatedmethodFilefileIn=newFile("D:\\ftest\\fin.txt");FilefileOut=newFile("D:\\ftest\\fout.txt");BufferedReaderreader=newBufferedReader(newBufferedWriterwriter=newBufferedWriter(newStringline="";Stringtext=while((line=reader.readLine())!=null){text+=line+"\n";} BufferedInputStreamreader1=newBufferedInputStream(new BufferedOutputStreamwriter1=newBufferedOutputStream(new intbytes= while((bytes=reader.read())!=- }3.4I/O編寫一個(gè)移動(dòng)文件程序,要求d:/test/a.txt文件移動(dòng)e:/me/test/ 將練習(xí)的1中的文件拷貝到 Windowsexerun()方法。Java1RunnablepublicclassThreadTestimplementsRunnableprivateStringpublicStringgetName()return}publicvoidsetName(Stringname)=}publicvoidrun()for(inti=0;i<10;i++){System.out.println(name+":"+i);}}publicstaticvoidmain(String[]args){ThreadTesttt1=newThreadTest();ThreadTesttt2=newtt2.setName("Threadt1=newThread(tt1);Threadt2=new}}publicclassThreadTest1extendsThreadprivateStringpublicThreadTest1(Stringname)=}publicvoidrun()for(inti=0;i<10000;i++){System.out.println(name+":"+i);}}publicstaticvoidmain(String[]args){ThreadTest1t1=newThreadTest1("");ThreadTest1t2=newThreadTest1("}}3java運(yùn)行狀態(tài)、阻塞狀態(tài)及狀態(tài)。新建狀態(tài)就緒狀態(tài)start()方法即啟動(dòng)了線程,start()方法創(chuàng)建線程運(yùn)行統(tǒng)的線程調(diào)度程序(threadscheduler)來調(diào)度的。運(yùn)行狀態(tài)CPUrun()阻塞狀態(tài)線程運(yùn)行過程中,可能由于進(jìn)入阻塞狀態(tài):1>sleep3>4>CPU狀態(tài)有兩個(gè)原因會(huì)導(dǎo)致線程run方法正常退出而自然runisAlivetrue;如果線程仍舊是new狀態(tài)且不是可運(yùn)行的,或者線程了,則返回false.同步方法上時(shí),自動(dòng)獲得與正在執(zhí)行代碼類的當(dāng)前實(shí)例(this)有關(guān)當(dāng)程序運(yùn)行到synchronized同步方法或代碼塊時(shí)才該對(duì)象鎖才起作synchronizedsynchronizedpublicintfix(inty)synchronized(this)x=x-}return}void voidnotifyAll()voidwait() notify()法或notifyAll()方法。wait()、notify()、notifyAll()Objectwait()方法獲

溫馨提示

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

評(píng)論

0/150

提交評(píng)論