java练习3组合和抽象练习

目的:面向对象之多态专项练习

题目:设计一个电子阅读器

描述:电子书的格式有txt、pdf、doc等多种,不同的格式对应不同的解析方法,程序需要根据传入的文件名后缀自动匹配对应的解析方法,进行显示。

要求:使用面向对象多态性进行设计,代码具有良好的可扩展性,方便扩充电子书格式。不要求进行真正的格式解析,用字符串示意即可,例如解析txt的方法,返回 “{文件名}的格式是txt”。

方法一:抽象类继承

public class Homework3 {
    public static void main(String[] args) {
        while(true){
            Scanner IN=new Scanner(System.in);
            System.out.println("请输入文件名:(按q退出)");
            String fileName=IN.nextLine();
            if(fileName.equals("q"))
                break;
            String fileType=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
            //substring(int begin,int end) begin-从0开始,左闭右开
            Reader read;
            switch(fileType){
                case "txt":
                    read=new txt();
                    read.Analyse();
                    break;
                case "pdf":
                    read=new pdf();
                    read.Analyse();
                    break;
                case "doc":
                    read=new doc();
                    read.Analyse();
                    break;
                default:
                    System.out.println("输入文件格式错误,请重新输入!");
            }
        }
    }
}

abstract class Reader{
    abstract void Analyse();
}

class txt extends Reader{
    @Override
    void Analyse() {
        System.out.println("完成txt解析");
    }
}

class pdf extends Reader{
    @Override
    void Analyse() {
        System.out.println("完成pdf解析");
    }
}

class doc extends Reader{
    @Override
    void Analyse() {
        System.out.println("完成doc解析");
    }
}

 方法二:组合

public class Homework3{
    public static void main(String[] args) {
        while (true){
            System.out.println("请输入要解析的文件:");
            Scanner in=new Scanner(System.in);
            String file=in.nextLine();
            String filename=file.substring(0,file.lastIndexOf("."));
            String type=file.substring(file.lastIndexOf(".")+1,file.length());
            Ebook ebook;
            switch (type){
                case "q": break;
                case "txt":
                    ebook=new Ebook(new txt());
                    ebook.Analyse(filename);
                    break;
                case "pdf":
                    ebook=new Ebook(new pdf());
                    ebook.Analyse(filename);
                    break;
                case "doc":
                    ebook=new Ebook(new doc());
                    ebook.Analyse(filename);
                    break;
                default:
                    System.out.println("输入文件错误,请重新输入:");;
            }
        }
    }
}

interface Read{
    void analyse(String s);
}

class txt implements Read{
    public void analyse(String s){
        System.out.println("解析txt文件"+" {"+s+"} "+"成功");
    }
}

class pdf implements Read{
    public void analyse(String s){
        System.out.println("解析pdf文件"+" {"+s+"} "+"成功");
    }
}

class doc implements Read{
    public void analyse(String s){
        System.out.println("解析doc文件"+" {"+s+"} "+"成功");
    }
}

class Ebook{
    Read reader;
    Ebook(Read reader){
        this.reader=reader;
    }
    void Analyse(String s){
        reader.analyse(s);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值