若某個方法的參數是接口類型,那麼可使用接口名和類體組合建立一個匿名對象,類體必需要重寫接口中的所有方法html
board.showMess(new OutputAlphabet() //向參數傳遞OutputAlphabet的匿名子類對象 { public void output() //改寫父類中output方法體 { for(char c='α';c<='ω';c++) //輸出希臘字母 System.out.printf("%3c",c); } } );
public String getMessage(); public void printStackTrace(); public String toString();
try{ 包含可能發生異常的語句 } catch(ExceptionSubClass1 e){//若try部分異常,即轉向catch部分 …… } catch(ExceptionSubClass2 e){ …… }
…… int n = 0,m = 0,t = 1000; try{ m = Integer.parseInt("8888"); n = Integer.parseInt("ab89"); //發生異常,轉向catch t = 7777; //t沒有機會被賦值,來不及執行就已經退出 } catch(NumberFormatException e) { System.out.println("發生異常:"+e.getMessage()); } ……
java Example7_6
java -ea Example7_6
import java.util.Scanner; public class Example7_6 { public static void main (String args[ ]) { int [] score={-120,98,89,120,99}; int sum=0; for(int number:score) { assert number>0:"負數不能是成績";//若是發現成績有負數,程序馬上結束運行 sum=sum+number; } System.out.println("總成績:"+sum); } }
File(String filename);//filename是文件名字
File(String directoryPath,String filename);//directoryPath是文件的路徑
File(File dir,String filename);//dir是一個目錄
File(String filename)
建立文件時,該文件被認爲與當前應用程序在同一目錄中in.close()
;FileInputStream(String name);
FileInputStream(File file);
File f=new File("Example10_4.java"); InputStream in = new FileInputStream(f);
FileOutputStream(String name);
FileOutputStream(File file);
void write(int n)
向目的地寫入單個字節。void write(byte b[])
向目的地寫入一個字節數組。void write(byte b[],int off,int len)
從字節數組中偏移量off處取len個字節寫到目的地。File file = new File("a.txt"); //輸出的目的地 OutputStream out=new FileOutputStream(file);
File sourceFile = new File("a.txt"); //讀取的文件 File targetFile = new File("b.txt"); //寫入的文件 Writer out = new FileWriter(targetFile,true); //指向目的地的輸出流 Reader in = new FileReader(sourceFile); //指向源的輸入流
BufferedReader(Reader in);
BufferedWriter (Writer out);
readLine()
讀取文本行write(String s,int off,int len)
把字符串s寫到文件中newLine();
向文件寫入一個回行符File fRead = new File("english.txt"); File fWrite = new File("englishCount.txt");//命名文件名 try{ Writer out = new FileWriter(fWrite);// **字符輸出,底層** BufferedWriter bufferWrite = new BufferedWriter(out); //輸出預備 Reader in = new FileReader(fRead);// **字符輸入,底層** BufferedReader bufferRead =new BufferedReader(in); //輸入預備 String str = null; while((str=bufferRead.readLine())!=null) { StringTokenizer fenxi = new StringTokenizer(str); int count=fenxi.countTokens();// **處理字符串有幾個詞(第八章)** str = str+" 句子中單詞個數:"+count; bufferWrite.write(str); bufferWrite.newLine(); } bufferWrite.close(); out.close(); // **先關高級流,再關底層流;Java規定只要關了前者,後者自動會關** in = new FileReader(fWrite); bufferRead =new BufferedReader(in); String s=null; System.out.println(fWrite.getName()+"內容:"); while((s=bufferRead.readLine())!=null) { System.out.println(s); } bufferRead.close(); in.close(); } catch(IOException e) { System.out.println(e.toString());
RandomAccessFile(String name,String mode) ;
(mode能夠是r或rw)RandomAccessFile(File file,String mode) ;
RandomAccessFile in=null; in=new RandomAccessFile("Example10_9.java","rw");
DataInputStream(InputStream in)
建立的數據輸入流指向一個由參數in指定的底層輸入流DataOutputStream(OutnputStream out)
建立的數據輸出流指向一個由參數out指定的底層輸出流File file=new File("apple.txt"); FileOutputStream fos=new FileOutputStream(file); DataOutputStream outData=new DataOutputStream(fos);
ObjectInputStream(InputStream in)
ObjectOutputStream(OutputStream out)
File file=new File("television.txt"); FileOutputStream fileOut=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(fileOut);
next()
依次返回file中的單詞hasNext()
判斷file最後一個單詞是否已被next()方法返回File file = new File("hello.java"); Scanner sc = new Scanner(file); //sc將空白做爲分隔標記
next()
依次返回file中的單詞hasNext()
判斷file最後一個單詞是否已被next()方法返回File file = new File("hello.java"); Scanner sc = new Scanner(file); sc.useDelimiter(正則表達式); //sc將正則表達式做爲分隔標記
若是有如下程序片斷java
interface Some{ void dosome (); } class SomeImpl implements Some { public void dosome(){ System.out.println("作一些事"); } } public class Main { public static void main (String [] args) { Some s= new SomeImpl(); s.dosome (); } }
如下描述正確的是
A .編譯失敗
B .顯示「作一些事」
C .發生ClassCastException
D .執行時不顯示任何信息
正確答案: B 你的答案: Ac++
博客中值得學習的或問題:git
有針對本身的問題積極尋找解決方法,和其餘同窗討論正則表達式
教材總結那裏能夠再精簡一點更好數組
基於評分標準,我給本博客打分:12分。得分狀況以下:正確使用Markdown語法(加1分);模板中的要素齊全(加1分);教材學習中的問題和解決過程, 一個問題加1分; 代碼調試中的問題和解決過程, 一個問題加1分;本週有效代碼超過300分行的(加2分);感想,體會不假大空的加1分;排版精美的加一分;進度條中記錄學習時間與改進狀況的加1分;代碼Commit Message規範的加1分;點評認真,能指出博客和代碼中的問題的加1分;結對學習狀況真實可信的加1分app
代碼行數(新增/累積) | 博客量(新增/累積) | 學習時間(新增/累積) | 重要成長 | |
---|---|---|---|---|
第一週 | 11/11 | 1/1 | ||
第二週 | 262/273 | 1/2 | ||
第三週 | 642/915 | 1/3 | ||
第四周 | 384/1299 | 2/5 | ||
第五週 | 661/1960 | 1/6 | ||
第六週 | 1031/2991 | 2/8 |