zoukankan      html  css  js  c++  java
  • JAVA 用接口模拟打印机,u盘接入USB

     1 package Demo512;
     2 
     3 import com.sun.org.apache.xml.internal.serialize.Printer;
     4 
     5 public class TestUsb {
     6     public static void main(String[] args) {
     7         Computer com= new Computer();
     8         com.doWord(new printer());
     9         com.doWord(new Flash());
    10 //        Flash f=new Flash();
    11 //        com.doWord(f);
    12         //实现了接口的匿名类的对象
    13     }
    14 }
    15 interface Usb{
    16     void start();
    17     void stop();
    18 }
    19 class Computer{
    20     public void doWord(Usb usb){
    21         usb.start();
    22         System.out.println("==此设备开始操作==");
    23         usb.stop();
    24     }
    25 }
    26 class printer implements Usb{
    27 
    28     @Override
    29     public void start() {
    30         System.out.println("打印机开始工作");
    31     }
    32 
    33     @Override
    34     public void stop() {
    35         System.out.println("打印机结束工作");
    36     }
    37 }
    38 class Flash implements Usb{
    39 
    40     @Override
    41     public void start() {
    42         System.out.println("u盘开始工作");
    43     }
    44 
    45     @Override
    46     public void stop() {
    47         System.out.println("u盘停止工作");
    48     }
    49 }

    运行代码↓

  • 相关阅读:
    建表关系
    设计模式-策略模式
    设计模式-适配器模式
    在线操作word和在线预览查找的资料记录
    Elasticsearch 使用-安装
    12-factors
    Kafka 使用-安装
    Apache Zookeeper 使用-安装
    Java 使用-安装
    设计模式-模板方法模式
  • 原文地址:https://www.cnblogs.com/Ssinoo/p/10853979.html
Copyright © 2011-2022 走看看