08-01 接口 異常 文件IO流

抽象類

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);

//郵箱 [email protected]|.cn|.net
Pattern p=Pattern.compile("^\\p{Alnum}{1,}\\@\\p{Alnum}+\\.(com|cn|net)$");
Matcher m=p.matcher("[email protected]");
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();
}
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 7464
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章