适配器
适配器要实现某一接口,也要实现某一类情况下使用。建立一个类,它继承该接口,继承一个类。
使用情况
1.系统需要使用现有的类,而此类的接口不符合系统的需要。
public interface Itarget
{
void request();
}
public class Adapatee
{
public void Specific()
{
Console.WriteLine(" Adapatee ");
}
}
//适配器
public class Adapater : Adapatee, Itarget
{
public void request()
{
this.Specific();
}
}
//实现方式
public static void Main(string[] args)
{
Itarget t=new Adapater();
t.request();
}
或者:
public class Target
{
public virtual void request();
}
public class Adapatee
{
public void Specific()
{
Console.WriteLine(" Adapatee ");
}
}
//适配器
public class Adapater : Target
{
Adapatee ad = new Adapatee();
public override void request()
{
ad.Specific();
}
}
适配器要实现某一接口,也要实现某一类情况下使用。建立一个类,它继承该接口,继承一个类。
使用情况
1.系统需要使用现有的类,而此类的接口不符合系统的需要。
public interface Itarget
{
void request();
}
public class Adapatee
{
public void Specific()
{
Console.WriteLine(" Adapatee ");
}
}
//适配器
public class Adapater : Adapatee, Itarget
{
public void request()
{
this.Specific();
}
}
//实现方式
public static void Main(string[] args)
{
Itarget t=new Adapater();
t.request();
}
或者:
public class Target
{
public virtual void request();
}
public class Adapatee
{
public void Specific()
{
Console.WriteLine(" Adapatee ");
}
}
//适配器
public class Adapater : Target
{
Adapatee ad = new Adapatee();
public override void request()
{
ad.Specific();
}
}
浙公网安备 33010602011771号