zoukankan      html  css  js  c++  java
  • day7_work1_printer

    作业

    面向接口编程思想的打印机:打印机,(黑.彩色墨盒),(a4.b5纸张)

    1. 定义接口:墨盒, 纸张
    2. 按接口开发类
    3. 将五个类按需要组装并使用

    类图:

    代码:

    墨盒接口:

    public interface Ink{
        public String getColor();
    }
    

    黑色墨盒类:

    public class BlackInk implements Ink{
        @Override
        public String getColor() {
        	return "黑色墨盒";
        }
    }
    

    彩色墨盒类:

    public class ColorInk implements Ink{
        @Override
        public String getColor() {
        	return "彩色墨盒";
        }
    }
    

    纸张接口:

    public interface Paper{
        public String getSize();
    }
    

    A4纸类:

    public class A4Paper implements Paper{
        @Override
        public String getSize() {
        	return "A4纸";
        }
    }
    

    B5纸类:

    public class B5Paper implements Paper{
        @Override
        public String getSize() {
        	return "B5纸";
        }
    }
    

    打印机类:

    public class Printer {
        private Paper paper;
        private Ink ink;
        private String brand;
        
        public Printer(String brand) {
            this.brand = brand;
        }
        
        public print(String words) {
        	System.out.println(brand + "牌打印机正在使用" + ink.getColor() + "和" + paper.getSize() + "打印:");
            System.out.println(words);
        }
        
        public void setPaper(Paper paper) {
            this.paper = paper;
        }
        
        public Paper getPaper() {
            return paper;
        }
        
    	public void setInk(Ink ink) {
            this.ink = ink;
        }
        
        public Ink getInk() {
            return ink;
        }
        public String getBrand() {
            return brand;
        }
    }
    

    客户类:

    public class Customer {
        public static void main(String[] args) {
            //采购
        	Paper a4Paper = new A4Paper();
            Paper b5Paper = new B5Paper();
            Ink blackInk = new BlackInk();
            Ink colorInk = new ColorInk();
            Printer printer = new Printer("海尔");
            //组装
            printer.setPaper(a4paper);
            printer.setInk(blackInk);
            //使用
            printer.print("呜呼");    
        }
    }
    

    运行结果

  • 相关阅读:
    2016huasacm暑假集训训练四 递推_A
    2016huasacm暑假集训训练三 G
    2016huasacm暑假集训训练三 F
    今年暑假不AC
    Who's in the Middle
    The Suspects
    食物链
    抓牛问题
    士兵队列训练问题
    H
  • 原文地址:https://www.cnblogs.com/zzz1zzz/p/14268977.html
Copyright © 2011-2022 走看看