class Ireader
{
public:
virtual string Getcontents() = 0;
};
class Book : public Ireader
{
public:
virtual string Getcontents()
{
return "读本书!";
}
};
class NewsPaper : public Ireader
{
public:
virtual string Getcontents()
{
return "读报纸!";
}
};
class EBook : public Ireader
{
public:
virtual string Getcontents()
{
return "读电子书!";
}
};
class Mother
{
public:
void tellStory(Ireader* pi)
{
cout << pi->Getcontents() << endl;
}
};
void TestDIP()
{
Mother m;
Book b;
NewsPaper n;
EBook eb;
m.tellStory(&b);
m.tellStory(&n);
m.tellStory(&eb);
}
C++依赖倒置原则
最新推荐文章于 2024-05-11 13:43:13 发布