08-01 接口 异常 文件IO流
2015-08-01 21:20
363 查看
抽象类
接口(简单)
接口(复杂)
自定义异常
文件异常
文件IO流、异常
内部类
局部类
匿名内部类
正则表达式
Data 、Calendar(常用的工具类)
public abstract class Animal { public abstract void voice(); public abstract void run(); } public class Dog extends Animal{ public void voice(){ System.out.println("汪汪"); } public void run(){ System.out.println("狗跑的很快"); } } public class Test { public static void main(String[] args) { Dog dog=new Dog(); dog.voice(); dog.run(); } }
接口(简单)
public interface Fly { public void fly(); } public interface Run { public void run(); } public class Plane implements Fly,Run{ @Override public void fly() { System.out.println("飞机用引擎飞"); } @Override public void run() { System.out.println("飞机起飞前先助跑"); } } public class Bird implements Fly{ @Override public void fly() { System.out.println("鸟用翅膀飞"); } } public class Car implements Run{ @Override public void run() { System.out.println("汽车直接启动,不用助跑"); } } public class Test { public static void main(String[] args) { Plane plane=new Plane(); plane.fly(); plane.run(); Bird bird=new Bird(); bird.fly(); Car car=new Car(); car.run(); } }
接口(复杂)
public interface Ink { public String getInkColor(); } public class InkSuzhou implements Ink{ @Override public String getInkColor() { return "黑色"; } } public class InkOther implements Ink{ @Override public String getInkColor() { return "红色"; } } public class InkFactory { public static Ink createink(){ InkSuzhou ink=new InkSuzhou(); return ink; } } public interface Paper { public String getPaperSize(); } public class PaperBeijing implements Paper{ @Override public String getPaperSize() { // TODO Auto-generated method stub return "A4"; } } public class PaperOther implements Paper{ @Override public String getPaperSize() { // TODO Auto-generated method stub return "B5"; } } public class PaperFactory { public static Paper createpaper(){ PaperBeijing paper=new PaperBeijing(); return paper; } } public interface Print { public void print(Paper paper,Ink ink); } public class PrintHP implements Print{ @Override public void print(Paper paper, Ink ink) { System.out.println("在"+paper.getPaperSize()+"纸上打印"+ink.getInkColor()+"内容"); //在A4打印黑色内容 } } public class PrintOther implements Print{ @Override public void print(Paper paper, Ink ink) { System.out.println("使用"+paper.getPaperSize()+"纸打印"+ink.getInkColor()+"内容"); //使用A4打印黑色内容 } } public class PrintFactory { public static Print createprint(){ PrintHP print=new PrintHP(); return print; } } public class Test { public static void main(String[] args) { Paper paper=PaperFactory.createpaper(); Ink ink=InkFactory.createink(); Print print=PrintFactory.createprint(); print.print(paper, ink); } }
自定义异常
public class Student { private int score; public int getScore() { return score; } public void setScore(int score) throws Failscores { if(score<60){ throw new Failscores(); } this.score = score; } } public class Failscores extends Exception{ public Failscores(){ super("不及格的异常"); } @Override public void printStackTrace() { System.err.println("成绩不理想"); super.printStackTrace(); } } public class Test { public static void main(String[] args) { /* //运行时异常 System.out.println(1/0); */ /* //空指针异常,一个引用变量,没有指向任何实际的对象 Student[] stu=new Student[10]; System.out.println(stu[0].getAge()); */ Student lisi=new Student(); System.out.println("程序开始运行"); try { lisi.setScore(50); } catch (Failscores e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("程序运行结束"); } }
文件异常
public class Test { public static void main(String[] args) { System.out.println("系统开始运行"); File file=new File("d://a.txt"); try { FileInputStream file1=new FileInputStream(file); file1.read(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //必须运行,不管是否捕获异常 System.out.println("处理完毕"); } } }
文件IO流、异常
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileTest { public static void main(String[] args) { File file=new File("d://s.txt"); if(file.exists()){ try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //文件写入 String words="踏天九境,踏天九桥"; try { byte[] array=words.getBytes(); FileOutputStream out=new FileOutputStream(file); out.write(array); out.flush(); out.close(); System.out.println("文件写入完成"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //文件输出 System.out.print("文件内容:"); byte[] array=new byte[1024]; try { FileInputStream in=new FileInputStream(file); int num=in.read(array); while(num!=-1){ System.out.println(new String(array, 0, num)); num=in.read(array); } in.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
内部类
//写在类内部的类 public class Student { private String name; private int age; public void study(){ System.out.println(this.name+"正在学习"); } public String getName() { System.out.println("姓名:"+name); return name; } public void setName(String name) { this.name = name; } public int getAge() { System.out.println("年龄:"+age); return age; } public void setAge(int age) { this.age = age; } public class Pen{ String name; String color; public void write(){ System.out.println(Student.this.name+"写点东西"); } public String getName() { System.out.println("类型:"+name); return name; } public void setName(String name) { this.name = name; } public String getColor() { System.out.println("颜色:"+color); return color; } public void setColor(String color) { this.color = color; } } } public class Test { public static void main(String[] args) { Student lisi=new Student(); lisi.setName("李斯"); lisi.setAge(18); System.out.println(lisi.getName()); System.out.println(lisi.getAge()); lisi.study(); Pen pen=lisi.new Pen(); pen.setName("钢笔"); pen.setColor("蓝色"); System.out.println(pen.getName()); System.out.println(pen.getColor()); pen.write(); } }
局部类
//写在方法中的类 public class Student { private String name; public void study(){ class Book{ String bookName="三国演义"; public void read(){ System.out.println(bookName); } } Book book=new Book(); System.out.print(Student.this.name+"正在学习"); book.read(); } } public class Test { public static void main(String[] args) { Student lisi=new Student(); lisi.setName("李斯"); lisi.study(); } }
匿名内部类
//匿名内部类,当只使用一次时 Paper paper=new Paper(){ @Override public String getPaperSize() { return "A4"; } };
正则表达式
//手机号 13 15 18 Pattern p=Pattern.compile("^1(3|5|8)\\d{9}$"); Matcher m=p.matcher("13445698735"); boolean b=m.matches(); System.out.println(b); //身份证号 18位,最后一位可能是x或X Pattern p=Pattern.compile("^\\d{17}(x|X|\\d)$"); Matcher m=p.matcher("13068219990205541x"); boolean b=m.matches(); System.out.println(b); //邮箱 xxxx@xxx.com|.cn|.net Pattern p=Pattern.compile("^\\p{Alnum}{1,}\\@\\p{Alnum}+\\.(com|cn|net)$"); Matcher m=p.matcher("asd@163.com"); boolean b=m.matches(); System.out.println(b);
Data 、Calendar(常用的工具类)
Date date=new Date(); System.out.println(date.toString()); System.out.println(date.getTime()); 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)); rightNow.set(Calendar.DAY_OF_YEAR, rightNow.get(Calendar.DAY_OF_YEAR)+5); System.out.println(rightNow.get(Calendar.DAY_OF_MONTH)); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println(format.format(date)); SimpleDateFormat format1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String time="2015年08月01日 16:00:26"; try { Date date1=format1.parse(time); System.out.println(date1.getTime()); } catch (ParseException e) { e.printStackTrace(); }
相关文章推荐
- Python简单的爬虫
- Web-服务器推送push——websocket、ajax轮询
- 部分32位Windows数据类型的介绍
- 亚信仨月印象(国庆时再写)
- 佛家经典禅语语录句子
- 佛家经典禅语语录句子
- 连续子数组求和
- MATLAB 基本数据类型和调用方式
- 做一个靠谱的iOS开发者(1)
- 在RedHat Server 5.4上安装YUM【利用网易免费yum源,配置和更新yum】
- 12.深浅拷贝
- Openfiler 配置iscsi iSCSI Targets 不能添加 Target IQN
- java培训 2015-07-31
- ISE中FPGA的实现流程
- What does => and () => mean in Scala
- Hibernate查询数据库的三种方式
- JDBC
- ABP之动态WebAPI(二)
- 测试连接MySQL数据库时遇到的一些问题
- CSUST 7月31日 并查集 & 最小生成树