1 / 23

Java Tutorial III

Java Tutorial III. 2004/12/02. 多型 (polymorphism). book. PDFBook. StoryBook. PaperBook. bookName ︰string price ︰int content ︰String ------------------------------- setBookName(String bookName) setBookPrice(int price) String getBookName() int getPrince() abstract void read()

Download Presentation

Java Tutorial III

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Tutorial III 2004/12/02

  2. 多型(polymorphism) book PDFBook StoryBook PaperBook bookName ︰string price ︰int content ︰String ------------------------------- setBookName(String bookName) setBookPrice(int price) String getBookName() int getPrince() abstract void read() read10time() displayText() displayText(String text)

  3. 多型(polymorphism)(cont1) • 利用抽象類別 達成概念與實作分離 • Book • PDFBook 是一種書 • PaperBook 是一種書 • StoryBook 是一種書 public class Book { String bookName; int price; String content; abstract void read(); void read10Time() { for (int count= 1;count<= 10;count++) { read(); } } }

  4. 多型(polymorphism)(cont2) public class PDFBook extends Book { public void read () { openFile(); displayText(); } private void openFile() { } public String readText() { contnet= readFileContent(); return content; } public void displayText() { displayText(readText()); } Public void displayText(boolean print) { displayText(readText(),0,true); } private void displayText(String text,int location,boolean print) { System.out.println(text); } }

  5. 多型(polymorphism)(cont3) public class people { private Book book1=null; private Book book2=null; public people( ) { book1=new StoryBook( ); book2=new PDFBook( ); } public void readBook( ) { book1.read( ); book2.read( ); book1.read10time( ); } } People BOOK Create(use) Result(response)

  6. 多型(polymorphism)(cont4) public class people { private Book bookList[ ]=new Book[3]; public people( ) { book[0]=new StoryBook( ); book[1]=new PDFBook( ); book[2]=new PaparBook( ); } public void readBook( ) { for (int count=0;count<bookList.length;count++) { book[count].read( ); } } } BOOK[0] BOOK[1] People BOOK[2]

  7. 多型(polymorphism)(cont5) public class people { private Book bookList[ ]=new Book[3]; public people( ) { book[0]=new StoryBook( ); book[1]=new PDFBook( ); book[2]=new PaparBook( ); } public void setRead(Object something) { for (int count=0;count<book.length;count++) { book[count].read(); } } } BOOK[0] BOOK[1] People BOOK[2]

  8. Java Object java.lang.Object 繼承 All Java Class public class people { public void setBook(Object something) { if (something instanceof Book) { something.read( ); } if (something instanceof Car) { something.drive( this ); } } }

  9. Exception • 程式執行過程,產生錯誤時,系統內部發出可供攔截的狀態 • 當程式內使用到會強迫發出Exception的物件時,需要使用try… catch… 指令攔截可能的Exception,沒有寫 try…catch…,程式將無法正確編譯(Compile Error) • 例如 • 檔案I/O的Class • FileInputStream • FileOutputStream • 多執行序 • Thread sleep method

  10. Exception(cont1) public class PDFBook extends Book { public void openFile( ) { try { FileInputStream fis=new FileInputStream(bookName); DataInputStream dis=new DataInputStream(fis); String st=“”; while ((st=dis.readLine())!=null) { content=content+st; } fis.close(); dis.close(); } catch (Exception e) { e.printStackTrace( ); } } }

  11. Exception(cont2) public class PDFBook extends Book { public void openFile( ) throws Exception { FileInputStream fis=new FileInputStream(bookName); DataInputStream dis=new DataInputStream(fis); String st=“”; while ((st=dis.readLine())!=null) { content=content+st; } fis.close(); dis.close(); } }

  12. All Object的概念 • Data Type 都使用物件建立 • Integer a,b,c; • int kk; • Double d,e,f; • double ff; • String x,y,z; • a=new Integer(3); • b=new Integer(5); • kk=a.intValue( )+b.intValue( ); • c=new Integer(kk); d=new Double(5.6); e=new Double(7.3); ff=c.doubleValue( )+d.doubleValue( ); f=new Double(ff); x=Integer.toString(a); Y=Double.toString(d); z=x+y+Double.toString(f); ff=Double.parseDouble(y);

  13. 常用的Java Package • java.lang.*; • java.lang.Runtime; • java.io.*; • java.util.*; • java.util.Date; • java.sql.*; • javax.sql.*; • javax.servlet.*; • java.awt.*;

  14. java.io.* • FileInputStream • DataInputStream • FileOutputStream • DataOutputStream • FileReader • FileWriter • BufferedInputStream • BufferedReader • BufferedOutputStream • BufferedWriter • 這些指令都會丟出Exception,要用 try… catch…

  15. java.util.* • ArrayList • Vector • HashMap • Hashtable • Iterator • Enumeration • Collections • Date

  16. java.util.Vector • 可以想像成就是LinkList • 常用的method • addElement(Object ) • add(Object ) • Object get(int ) • contains(Object ) • size( ) • removeAllElements( ) • removeElement(Object ) • removeElement(int ) • setElementAt(Object, int ) • insertElementAt(Object, int)

  17. java.util.ArrayList • 概念與LinkList相同 • 與Vector不同點在於,ArrayList沒有實作synchronize • 優點︰操作資料速度較快 • 缺點︰要自己注意資料同步的問題 • 沒有多工同步的問題時,使用ArrayList • addAll(Collection )

  18. java.util.HashMap • 可以想像成是記憶體資料庫 • 常用的指令 • put(key, value) • Object get(key) public class Database { private HashMap db=new HashMap( ); public void insert(Object key, Object value) { db.put(key,value); } public Objec getValue(Object key) { return db.get(key); } }

  19. java.util.Hashtable • 與HashMap概念相同 • 最大的不同點 • Hashtable的key值不可為null • Hashtable支援同步 public class Database { private Hashtable db=new Hashtable( ); public void insert(Object key, Object value) { db.put(key,value); } public Objec getValue(Object key) { return db.get(key); } }

  20. java.lang.Runtime • 當需要呼叫外部程式的時候,可使用java.lang.Runtime來執行外部指令的目的 public class Blast { public void runBlast( ) { Runtime rt=null; Process proc=null; String blastCommand= blastpath + "blastall" + " -p blastp“+" -d " + blastpath + "db" + STR_DIR + "DIP_HUMAN“+" -i " + aafile + " -o " + human_blastfile + " -e 0.001 -T T"; rt=Runtime.getRuntime(); proc=rt.exec(blastCommand); } }

  21. Web Site Development • Homepage • JSP • Java Servlet • execute outer program • getRuntime • thread

  22. Apache Project • Jakarta-tomcat • Java Server Page • Httpd web server

  23. Web Server • Listen 80 Port (Default) • 可以調整 • Jakarta-tomcat內定web content目錄 • webapps • Httpd內定web content目錄 • Htdocs • 程式執行目錄 • cgi-bin

More Related