依赖倒置原则:高层模块不应该依赖低层模块,两者应该依赖抽象(抽象类,接口)
class Program
{
static void Main(string[] args)
{
Moter mother = new Moter();
mother.Narrate(new Books());
mother.Narrate(new NewsPaper());
Console.ReadKey();
}
public interface IReader
{
string GetContent();
}
public class Books : IReader
{
public string GetContent()
{
return "有一个故事发生在很久以前";
}
}
public class NewsPaper:IReader
{
public string GetContent()
{
return "春节到了,欢度新年";
}
}
public class Moter
{
public void Narrate(IReader reader)
{
Console.WriteLine("开始讲故事");
Console.WriteLine(reader.GetContent());
}
}
}