笔记
内部类
public class Student extends Person {
private int score;
private int id;
public void study() {
// 局部内部类,只能在方法中用?
class Book {
String bookName = "大话设计模式";
public void read() {
System.out.println("在读" + bookName);
}
}
Book book = new Book();
book.read();
System.out.println("student:" + getName() + " age:" + getAge() + " study, yes score:" + getScore());
}
// 成员内部类
class Pen {
String name;
String color;
public void write() {
System.out.println(Student.this.getName() + "用" + getColor() + getName() + "写字");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
}
// 匿名内部类
public class Test {
// 书《大话设计模式》
public static void main(String[] args) {
// 面向接口
Paper paper = PaperCreate.createPaper();
// 匿名内部类,当只使用一次这个对象的时候使用,因为匿名类没有名字,只能创建一次对象
Paper paper2 = new Paper(){
@Override
public String getPaper() {
return "A3";
}
};
Ink ink = InkCreate.createInk();
Print print = PrintCreate.createPrint();
print.print(paper2, ink);
}
}
接口
// 一个接口
public interface Ink {
public String getInk();
}
// 面向接口,接口是完全抽象的类,只有抽象方法
异常
/*
* 异常捕获
* 运行时异常 是可以在写代码时改正的
* 非运行时异常是不可避免的,必须用try{}catch{}语句块
* throws抛出异常
* throw自定义异常
*/
File file = new File("e://新建文本文档.txt");
try {
FileInputStream f = new FileInputStream(file);
f.read();
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO异常");
e.printStackTrace();
} finally { // finally 是不管是否捕获异常都要执行的语句
System.out.println("不管是否捕获异常都要执行的语句");
}
System.out.println("程序结束");
// 自定义异常
public class FailScore extends Exception{
public FailScore() {
super("you failed in your exam !");
}
@Override
public void printStackTrace() {
System.err.println("切记!60分万岁");
super.printStackTrace();
}
}
学生类的部分代码
// 抛出异常,在调用setScore()方法的地方处理异常
public void setScore(int score) throws FailScore {
if (score < 60) {
// 抛出异常
throw new FailScore();
} else {
this.score = score;
}
}
Student san = new Student();
try {
san.setScore(45);
} catch (FailScore e) {
System.out.println("捕获异常");
e.printStackTrace();
}
正则表达式
// 正则表达式的一般调用,Pattern,Macher,boolean结果
import java.util.regex.Matcher;
import java.util.regex.Pattern;
............
Pattern p = Pattern.compile("^1(3|4|5|7|8)\\d{9}$");
Matcher m = p.matcher(input.next());
boolean b = m.matches();
System.out.println(b);
Pattern p1 = Pattern.compile("^\\d{17}(X|\\d)$");
Matcher m1 = p1.matcher(input.next());
boolean b1 = m1.matches();
System.out.println(b1);
// 判断.的格式\\.
Pattern p2 = Pattern.compile("^\\d{5,11}@[a-z[0-9]]{2,3}\\.(com|cn|net)$");
Matcher m2 = p2.matcher(input.next());
boolean b2 = m2.matches();
System.out.println(b2);
文件输入输出流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
File file = new File("e://新建文本文档.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String words = "xieixie";
try {
byte[] array = words.getBytes();
FileOutputStream f = new FileOutputStream(file);
f.write(array);
f.flush();
f.close();// 写完后关闭文件
System.out.println("写入完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] array = new byte[1024];
try {
FileInputStream f = new FileInputStream(file);
int num = f.read(array);
while(num != -1) {
System.out.println(new String(array,0,num));
num = f.read(array);
}
f.close(); // 读完文件关闭文件,释放内存
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
常用类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date date = new Date();
// 返回从1970年1月1日到当前时间的毫秒数
long time = date.getTime();
System.out.println(time);
// 输出当前时间格式 星期 月 日 时:分:秒 时区 年
System.out.println(date.toString());
// Calendar
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.get(Calendar.YEAR));
System.out.println(rightNow.get(Calendar.MONTH) + 1);
System.out.println(rightNow.get(Calendar.DAY_OF_MONTH));
// 格式化输出
SimpleDateFormat format = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
String time1 = format.format(rightNow.getTime());
System.out.println(time1);
// 将格式化的时间变为毫秒数
String time2 = "2015/08/01 16:09:04";
try {
Date date1 = format.parse(time2);
System.out.println(date1.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
}
总结
学的都没逻辑了,脑袋好乱。
我想jingjing