#目录
抽象类
接口
内部类
常用类
异常
IO数据流
##抽象类
abstract
抽象类:不一定有抽象方法,但有抽象方法的一定是抽象类。
抽象方法:只有方法名,没有方法体
抽象类不能new出一个对象
继承抽象类的子类必须实现抽象方法,如果没有实现,子类也是一个抽象类。
/**
* Animal为abstract抽象类,只能被继承,不能new出对象
*
* @author Administrator
*
*/
public abstract class Animal {
public abstract void voice();// 抽象方法
}
/**
* 子类如果没有重写父类中的抽象方法,那么子类也是抽象类
*
* @author Administrator
*
*/
public class Dog extends Animal {
// 重写的抽象类的抽象方法
@Override
public void voice() {
System.out.println("汪汪汪~");
}
}
/**
* 测试类
*
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
Animal animal = new Dog();// 父类引用子类对象
animal.voice();
}
}
##接口 面向接口编程 ***interface*** 接口:全部是抽象方法的类 一个类可以实现多个接口,中间用“,”隔开。 接口的引用指向实现此接口的对象 ```java /** * 创建Ink,Paper,Print接口 * @author Administrator * */ public interface Ink { public String ink(); }
public interface Paper {
public String paper();
}
public interface Print {
public void print(Paper paper,Ink ink);
}
public class InkOther implements Ink {
public String ink() {
return “彩色”;
}
}
public class InkQingdao implements Ink {
public String ink() {
return “黑色”;
}
}
public class PaperBeijing implements Paper {
@Override
public String paper() {
return “A4和B5”;
}
}
public class PaperShanghai implements Paper{
@Override
public String paper() {
return “A4”;
}
}
public class PrintHP implements Print{
public void print(Paper paper, Ink ink) {
//通过对象,调用类的方法
System.out.println(“使用”+paper.paper()+“大小的纸和”+ink.ink()+“墨打印”);
}
}
public class PrintOther implements Print{
@Override
public void print(Paper paper, Ink ink) {
System.out.println(“使用”+ink.ink()+“的墨在”+paper.paper()+“大小的纸上打印”);
}
}
/**
- 定义一个类,方法为返回一个其他类对象。
- @author Administrator
*/
public class PaperFactor {
public Paper createrPaper(String factor) {
switch (factor) {
case “北京纸厂”:
return new PaperBeijing();
case “上海纸厂”:
return new PaperShanghai();
default:
return null;
}
}
}
import java.util.Scanner;
//测试类
public class Test {
public static void main(String[] args) {
System.out.println(“请选择:上海纸厂or北京纸厂”);
Scanner scanner = new Scanner(System.in);
Paper paper = new PaperFactor().createrPaper(scanner.next());
Ink ink = new InkOther();
Print print = new PrintHP();
// 接口引用指向实现此接口的对象
print.print(paper, ink);
}
}

<a id = "innerClass">
##内部类
内部类:指的是对象包含的类创建对象
类名 对象名 = 外部类.new 内部类名
```java
public class Student{
class Pen {
String color;
String name;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void write() {
// 内部类调用外部类属性时 用外部类名.this.外部类属性或对象
System.out.println(Student.this.getName() + "正在用" + getColor() + "笔写字");
}
}
}
内部类调用外部类属性时 用外部类名.this.外部类属性或对象
局部类:在方法中的类,创建对象时只能在方法中创建对象。
public void study() {
class Books {// 局部类,在方法中写的类
String bookName;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void read() {
System.out.println("正在读书~" + "读的是" + bookName);
}
}
Books book = new Books();// 只能在方法中创建对象
book.setBookName("Java基础");
book.read();
System.out.println("我正在学习");
}
匿名内部类:
当只使用一次这个类的对象的时候使用
因为这个类没有名字,所以只能创建一次对象
Paper paper = new Paper() {
@Override
public String paper() {
mo();
return "B5";
}
// 在匿名内部类中新增的方法只能在内中原有的方法中调用。
public void mo() {
System.out.println("默写纸张");
}
};
完整程序的结果
Date 日期类
// Date类 查看API
Date date = new Date();
System.out.println(date + "\t" + date.getTime());
Calendar 日历类
// Calendar日历类不能new对象
Calendar calendar = Calendar.getInstance();
Date date2 = calendar.getTime();// 将Calendar转化成Date
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
System.out.
Calendar的set方法
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 5); System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
时间的格式化输出
// 格式化输出
// SimpleDateFormat xxxx年xx月xx日 xx:xx:xx
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//将Date转换成字符串
System.out.println(dateFormat.format(calendar.getTime()));
//将字符串转换成Date
String time = "2015年08月01日 16:06:27";
try {
Date date1 = dateFormat.parse(time);
System.out.println(date1);
} catch (ParseException e) {
e.printStackTrace();
}

####正则表达式
查看API
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern p = Pattern.compile("^1(3|5|7|8|4)\\p{Digit}{9}$");
//11位手机号 开头13 15 17 18 14 必须跟9位数字
Matcher m = p.matcher("18564590249");
boolean b = m.matches();
System.out.println(b);
// 写一个省份证号 18位 最后一位可能为x或X
Pattern pattern = Pattern.compile("^\\p{Digit}{17}[0-9xX]$");
Matcher matcher = pattern.matcher("37035519940602191x");
boolean b2 = matcher.matches();
System.out.println(b2);
// 邮箱地址xxxx@xxx.com|cn|net
Pattern pattern2 = Pattern.compile("^\\p{Alnum}{1,}\\@\\p{Alnum}+\\.(com|cn|net)$");
Matcher matcher2 = pattern2.matcher("xxxxxx@qq.com");
boolean b3 = matcher2.matches();
System.out.println(b3);
}
}
测试结果
##异常
Exception(捕获)
runtimeException
非runtimeException
try{可能出现异常的代码}
catch(Exception e){捕获异常后的处理}
catch的连用:
catch(){}
catch(范围大的){}范围大的放下边
finally 不管是否捕获异常必须运行
抛出异常throws往上一级抛出
自定义异常
继承Exception
/**
* 自定义异常类
* @author Administrator
*
*/
public class FailScores extends Exception{
public FailScores() {
super("不及格的异常");
}
@Override
public void printStackTrace() {
System.err.println("成绩不理想,继续努力");
super.printStackTrace();
}
}
public class Student {
private int score;
//继续向上抛出异常 用throws
public void getScore(int score) throws FailScores {
this.score = score;
if (score<60) {
//抛出异常
throw new FailScores();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
testException();
// -------------------------------
// 自定义异常测试
testStudent();
}
public static void testException() {
File file = new File("d://11.txt");
try {
// 可能会出现异常的代码
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read();
} catch (FileNotFoundException e) {
// 捕获异常后的处理
e.printStackTrace();
System.out.println("该文件不存在");
} catch (IOException e) {// 比上边捕获到的异常作用域大
e.printStackTrace();
} finally {
System.out.println("处理完毕");
}
}
public static void testStudent() {
System.out.println("程序开始运行");
Student student = new Student();
try {
student.getScore(50);
} catch (FailScores e) {
e.printStackTrace();
}
System.out.println("程序运行结束");
}
}
运行结果
##IO数据流
File
InputStream 记得循环 close()
OutputStream 最后记得flush() close()
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
// 创建文件 注意是否存在
File file = new File("d://11.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 向文件中写数据 FileOutPutStream
String words = "我要写数据啦";
byte[] array = words.getBytes();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(array);
fileOutputStream.flush();// 注意一定要写
fileOutputStream.close();// 注意一定要写
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读数据 FileInputStream
byte[] arrays = new byte[1024];
try {
FileInputStream fileInputStream = new FileInputStream(file);
int num = fileInputStream.read(arrays);
if (num != -1) {// 通过循环输出所有数据
System.out.println(new String(arrays));
num = fileInputStream.read(arrays);
}
fileInputStream.close();// 注意一定要写
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果