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

//邮箱 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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值