标签:
1、先写接口
interface smartDevice //智能设备接口
{
public void saveData(String data); //保存数据方
法
}
interface USB //USB接口
{
public void insert();
public String read();
public void write(String s);
public void pop();
}
2、再构建具体的类
public class Computer implements SmartDevice
{
private USB usbDevice;
public Computer(){} //这里不能构造USB设备
public void saveData(String data) //实现智能设
备的方法
{
//insert,write,pop
}
//装配USB设备到电脑
public void setUsbDevice(USB u) { usbDevice=u;}
}
public class UDisk implements USB //U盘
{
public void insert(){...}
public String read(){...}
public void write(String s){...}
public void pop(){...}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="theUDisk" class="com.UDisk" abstract="false"
lazy-init="default" autowire="default" >
</bean>
<bean id="theComputer" class="com.Computer" abstract="false"
lazy-init="default" autowire="default"
>
<property name="usbDevice" ref="theUDisk"/>
</bean></beans>
4、主函数调用
public class test {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "beans.xml", "applicationContext.xml" });
Computer c = (Computer) applicationContext.getBean("theComputer");
c.saveData("个人简历");
}
}
标签:
原文地址:http://www.cnblogs.com/z-j-q/p/4519231.html