• 设计模式适配器模式


    ------------------------对象适配器模式(组合模式)---------------------------------
    public class AdapterTest {
    public static void main(String[] args) {
    Target target = new Adapter(new Adaptee());
    target.output5v();
    }
    }

    class Adaptee {
    public int output220v() {
    return 220;
    }
    }

    interface Target {
    int output5v();
    }

    class Adapter implements Target {
    private Adaptee adaptee;
    public Adapter(Adaptee adaptee) {
    this.adaptee = adaptee;
    }
    @Override
    public int output5v() {
    System.out.println("原始电压"+ adaptee.output220v() + ", 转换后电压: " + 5);
    return 5;
    }
    }

    --------------------------------类适配器模式(继承)-----------------------------------
    public class AdapterTest {
    public static void main(String[] args) {
    Target target = new Adapter();
    target.output5v();
    }
    }

    class Adaptee {
    public int output220v() {
    return 220;
    }
    }

    interface Target {
    int output5v();
    }

    class Adapter extends Adaptee implements Target {
    @Override
    public int output5v() {
    System.out.println("原始电压"+ output220v() + ", 转换后电压: " + 5);
    return 5;
    }
    }
     
  • 相关阅读:
    zipfile和tarfile的简单使用方法
    RabbitMQ安装
    postman接口自动化
    linux命令
    redis安装部署和使用
    nmon使用
    jdk自带监控工具配置使用
    修改本机mac
    hashlib模块,md5加密
    tomcat部署
  • 原文地址:https://www.cnblogs.com/ladeng19/p/15935217.html
Copyright © 2020-2023  润新知