若某个方法的参数是接口类型,那么可使用接口名和类体组合建立一个匿名对象,类体必需要重写接口中的所有方法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 |