博客
关于我
C# 适配器模式
阅读量:356 次
发布时间:2019-03-04

本文共 1053 字,大约阅读时间需要 3 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 适配器模式{    class Program    {        static void Main(string[] args)        {            Target target = new Adapter();            target.Request();            Console.ReadKey();        }    }    ///     /// 定义客户端期待的接口    ///     public class Target    {        ///         /// 使用virtual修饰以便子类可以重写        ///         public virtual void Request()        {            Console.WriteLine("这是Target类的Request方法");        }    }    ///     /// 定义需要适配的类    ///     public class Adaptee    {        public void SpecificRequest()        {            Console.WriteLine("这是Adaptee类的SpecificRequest方法");        }    }    ///     /// 定义适配器    ///     public class Adapter : Target    {        // 建立一个私有的Adeptee对象        private Adaptee adaptee = new Adaptee();        ///         /// 通过重写,表面上调用Request()方法,变成了实际调用SpecificRequest()        ///         public override void Request()        {            adaptee.SpecificRequest();        }    }}

运行:

 

转载地址:http://gwgr.baihongyu.com/

你可能感兴趣的文章
Jquery添加元素
查看>>
Jquery使用需要下载的文件
查看>>
Spring中如何传递参数的问题
查看>>
BST中某一层的所有节点(宽度优先搜索)
查看>>
广度优先搜索
查看>>
猜字母
查看>>
Eclipse导出项目出现resource is out of sync with the file...错误
查看>>
Linux网络环境配置(设置ip地址)
查看>>
Idea使用Spring Initializr来快速创建springboot项目
查看>>
C++邻接表存储图的深度优先搜索
查看>>
Dijkstra算法的总结
查看>>
前后端通信问题 —— SpringBoot+LayUI
查看>>
ubuntu中安装scikit-learn
查看>>
面向对象的三大特征
查看>>
SpringCloud和SprinBoot之间的关系
查看>>
剑指offer打卡Day14:数组中只出现一次的数字
查看>>
maven打包可执行文件jar
查看>>
javascript定义变量及数据类型介绍
查看>>
C语言的运算符和表达式
查看>>
【模拟】优美三角剖分
查看>>