嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
用C#实现一个简单的适配器模式的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdapterPatternTest
{
interface IJob {
void speakChinese();
void speakEnglish();
}
class Person {
public void speakChinese1() {
Console.WriteLine("I can speak Chinese");
}
}
class AdapterOfClass : Person, IJob {
public void speakChinese() {
Console.WriteLine("类适配器通过继承获得Person类的speakChinese方法");
base.speakChinese1();
}
public void speakEnglish() {
Console.WriteLine("the person can speak English through the AdapterOfClass");
}
}
class AdapterOfObject : IJob {
private Person person;
public AdapterOfObject(Person person) {
this.person = person;
}
public void speakChinese() {
Console.WriteLine("对象适配器通过获得一个Person类实例来取speakChinese方法");
person.speakChinese1();
}
public void speakEnglish() {
Console.WriteLine("the person can speak English through the AdapterOfObject");
}
}
public class Program
{
static void Main(string[] args)
{
IJob job1 = new AdapterOfClass();
Console.WriteLine("类适配器演示开始:");
job1.speakChinese();
job1.speakEnglish();
Console.WriteLine("类适配器演示结束");
IJob job2 = new AdapterOfObject(new Person());
Console.WriteLine("对象适配器演示开始:");
job2.speakChinese();
job2.speakEnglish();
Console.WriteLine("对象适配器演示结束");
Console.ReadKey();
}
}
}