Spring ioc 小案例,面向接口编写。

本文介绍了一种基于接口的打印机编程设计,通过定义Ink和Paper接口,实现不同颜色和纸张类型的打印功能。具体包括ColorInk和GreyInk类实现颜色打印,TextPaper类实现纸张内容管理和换行换页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


注:【网页版】右上方的悬浮框( 有目录索引 )


一、打印机选择墨盒与纸张进行打印

1-1】效果图 及 包结构

效果图包结构
在这里插入图片描述在这里插入图片描述

1-2】接口 与 类

com.printer 包下

public interface Ink {
    public String getColor(int r,int g,int b);
}
public interface Paper {

    // 换行标准
    public static final String newLine="\r\n";

    // 往纸张里面逐个字符输入内容
    public void putInChar(char c);

    // 得到纸张中的内容
    public String getContent();
}
public class Printer {

    // 面向接口编程,定义 接口变量
    private Ink ink;
    private Paper paper;

    public Ink getInk() { return ink; }
    public void setInk(Ink ink) { this.ink = ink; }
    public Paper getPaper() { return paper; }
    public void setPaper(Paper paper) { this.paper = paper; }

    public void print(String str){
        System.out.println("使用"+ink.getColor(225,220,5)+"颜色打印");
        for(int i=0;i<str.length();i++){
            paper.putInChar(str.charAt(i));
        }
        System.out.println(paper.getContent());
    }
}

com.ink 包下

public class ColorInk implements Ink {
    @Override
    public String getColor(int r, int g, int b) {
        Color color = new Color(r,g,b);
        return "#"+Integer.toHexString(color.getRGB()).substring(2);
    }
}
public class GreyInk implements Ink {
    @Override
    public String getColor(int r, int g, int b) {
        int c = (r+g+b)/3;
        Color color = new Color(c,c,c);
        return "#"+Integer.toHexString(color.getRGB()).substring(2);
    }
}
public class TextPaper implements Paper {

    private int charPerLine = 16;   // 一行有 16 字符
    private int linePerPage = 8;    // 一张有 18 行
    private int posx = 0;   // 横向位置 从 0 至 charPerLine-1
    private int posy = 0;   // 当前行号
    private int posp = 1;   // 当前页号
    private String content = "";

    public int getCharPerLine() {
        return charPerLine;
    }
    public void setCharPerLine(int charPerLine) {
        this.charPerLine = charPerLine;
    }
    public int getLinePerPage() {
        return linePerPage;
    }
    public void setLinePerPage(int linePerPage) {
        this.linePerPage = linePerPage;
    }

    @Override
    public void putInChar(char c) {
        content +=c;
        ++posx;
        // 判断换行
        if(posx == charPerLine){
            content += Paper.newLine;
            posx = 0;
            ++posy;
        }
        // 判断换页
        if(posy == linePerPage){
            content +="===第"+posp+"页===";
            content += Paper.newLine + Paper.newLine;
            posy = 0;
            ++posp;
        }
    }

    @Override
    public String getContent() {
        // 为当前页加上页号
        String ret = content;
        if(!(posx==0&&posy==0)){
            // 当前页不为空
            int count = linePerPage - posy;
            for (int i=0;i < count;i++){
                // 为了保证每一页大小一致。给空的行,添加换行
                ret += Paper.newLine + Paper.newLine;
            }
            ret +="===第"+posp+"页===";
        }
        return ret;
    }
}

1-3】配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.ink.GreyInk" id="greyInk"/>
    <bean class="com.ink.ColorInk" id="colorInk"/>
    <bean class="com.ink.TextPaper" id="paperA4">
        <property name="charPerLine" value="8"/>
        <property name="linePerPage" value="6"/>
    </bean>
    <bean class="com.ink.TextPaper" id="paperB5">
        <property name="charPerLine" value="6"/>
        <property name="linePerPage" value="5"/>
    </bean>
    <bean class="com.printer.Printer" id="printer">
        <property name="ink" ref="greyInk"/>
        <property name="paper" ref="paperB5"/>
    </bean>
</beans>

1-4】测试类

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Printer printer = (Printer) context.getBean("printer");
        printer.print("一支玫瑰入眼眸,一壶烈酒融入喉,一缕清风伴长久" +
                "思念如同循环小数,一遍一遍执迷不悟" +
                "伊茉琳是一个恶魔,以他人的痛苦为生,并以此为乐" +
                "即是金克斯也会专注于一件事,冷静下来摧毁对面水晶");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值