資源簡介
用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;
?????????
評論
共有 條評論