问题描述
对于不同系统的手机,软件基本无法兼容,因此如果手机需要增加软件,就需要针对不同品牌的手机分别实现软件功能。
代码实现
手机软件抽象类
public abstract class HandsetSoft
{
public abstract void Run();
}
手机软件具体实现类
public class HandsetGame : HandsetSoft
{
public override void Run()
{
Console.WriteLine("运行手机游戏");
}
}
public class HandsetAddressList : HandsetSoft
{
public override void Run()
{
Console.WriteLine("运行手机通讯录");
}
}
手机抽象类
public abstract class HandsetBrand
{
protected HandsetSoft soft;
public void SetHandsetSoft(HandsetSoft soft)
{
this.soft = soft;
}
public abstract void Run();
}
手机具体实现类
public class HandsetBrandXiaomi : HandsetBrand
{
public override void Run()
{
soft.Run();
}
}
public class HandsetBrandIPhone : HandsetBrand
{
public override void Run()
{
soft.Run();
}
}
Main方法调用
internal class Program
{
static void Main(string[] args)
{
HandsetBrand brandMi = new HandsetBrandXiaomi();
brandMi.SetHandsetSoft(new HandsetGame());
brandMi.Run();
brandMi.SetHandsetSoft(new HandsetAddressList());
brandMi.Run();
HandsetBrand brandIphone = new HandsetBrandIPhone();
brandIphone.SetHandsetSoft(new HandsetGame());
brandIphone.Run();
brandIphone.SetHandsetSoft(new HandsetAddressList());
brandIphone.Run();
}
}