题干
已知接口ink和paper,ink中有抽象函数print,paper中有抽象函数putChar(),已经编写好这两个接口的实现类,黑白墨盒类和 A4纸类,在墨盒类中重写抽象函数即可,纸类中需要定义rows和cols成员变量,代码如下
Ink接口
package com.hkd.printer;
public interface Ink {
public String getColor();
}
Paper接口
package com.hkd.printer;
public interface Paper {
public void putChar();
}
墨盒实现类
package com.hkd.printer;
public class GreyInkImp implements Ink {
public String getColor() {
return "黑白打印机墨盒";
}
}
A4纸类
package com.hkd.printer;
public class A4Paper implements Paper {
private int rows;
private int cols;
private String str;
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getCols() {
return cols;
}
public void setCols(int cols) {
this.cols = cols;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public void putChar() {
// TODO Auto-generated method stub
System.out.println(this.rows+";"+this.cols);
System.out.println(str);
}
}
要求编写完成如下代码:
1.编写打印机类,使用墨盒类和纸类来进行组装,并且编写打印机类的打印函数。
2.使用IOC方式,在工厂文件中进行组件的生产(头文件如下所示)。
3.使用单元测试进行测试。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
做题步骤:
将上述代码,放到相应包下。
1.打印机类
package com.hkd.printer;
public class Dayinji {
//记
private A4Paper a4Paper;
private GreyInkImp greyInkImp;
//自动生成
public A4Paper getA4Paper() {
return a4Paper;
}
public void setA4Paper(A4Paper a4Paper) {
this.a4Paper = a4Paper;
}
public GreyInkImp getGreyInkImp() {
return greyInkImp;
}
public void setGreyInkImp(GreyInkImp greyInkImp) {
this.greyInkImp = greyInkImp;
}
//开始打印的函数
public void start(){
System.out.println("正在打印");
System.out.println("墨盒:"+greyInkImp.getColor());
System.out.println("纸张:"+a4Paper.getRows()+"行,"+a4Paper.getCols()+"列,"+a4Paper.getStr());
System.out.println("打印完成");
}
}
2.在工厂文件中进行组件的生产
<?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-4.3.xsd">
<!-- 配置 -->
<bean id="a4Paper" class="com.hkd.printer.A4Paper">
<property name="rows" value="5"></property>
<property name="cols" value="7"></property>
<property name="str" value="正在打印A4紙"></property>
</bean>
<bean id="greyInkImp" class="com.hkd.printer.GreyInkImp"></bean>
<bean id="dayinji" class="com.hkd.printer.Dayinji">
<property name="a4Paper" ref="a4Paper"></property>
<property name="greyInkImp" ref="greyInkImp"></property>
</bean>
</beans>
3.测试
package com.hkd.printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Dayinji bean = applicationContext.getBean("dayinji",Dayinji.class);
bean.start();
}
}