Spring-IOC

ink层Colorlnk

package cn.happy.printer.ink;

/**
 * Created by Happy on 2017-07-23.
 */
public class ColorInk implements Ink {
    public String getColor() {
        return "彩色墨盒";
    }
}

ink层GrayInk

package cn.happy.printer.ink;

/**
 * Created by Happy on 2017-07-23.
 */
public class GrayInk implements Ink {
    public String getColor() {
        return "黑白墨盒";
    }
}


ink层Ink接口

package cn.happy.printer.ink;

/**
 * Created by Happy on 2017-07-23.
 * 墨盒接口
 */
public interface Ink {
    //获取颜色的方法
    public String getColor();
}


paper层A4Paper

package cn.happy.printer.paper;

/**
 * Created by lenovo on 2017/7/23.
 */
public class A4Paper implements Paper {
    public String getpaper() {
        return "A4纸";
    }
}

paper层B5Paper

package cn.happy.printer.paper;

/**
 * Created by lenovo on 2017/7/23.
 */
public class B5Paper implements Paper{
    public String getpaper() {
        return "B5纸";
    }
}


paper层Paper接口

package cn.happy.printer.paper;

/**
 * Created by lenovo on 2017/7/23.
 */
public interface Paper {
    public String getpaper();
}


prink层Printer

package cn.happy.printer.print;

import cn.happy.printer.ink.Ink;
import cn.happy.printer.paper.Paper;

/**
 * Created by lenovo on 2017/7/23.
 */
public class Printer {
    private Ink ink;
    private Paper paper;
    public void print(){
        System.out.println("用"+ink.getColor()+"\t颜色的墨盒在"+paper.getpaper()+"\t类型的纸张上打印出:我爱你中国");
    }

    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 class SpringTest0723 {
    @Test
    //打印机案例
    public void test02(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //装配出一台打印机
        Printer pp=  (Printer)ctx.getBean("pinter");
        pp.print();
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值