zoukankan      html  css  js  c++  java
  • Open closed principle

    #include <iostream>
    
    using namespace std;
    
    class Book
    {
    public:
        string getContents()
        {
            return "Long time ago,There is a temple in the mountain";
        }
    };
    
    class Paper
    {
    public:
        string getContents()
        {
            return "I am handsome";
        }
    };
    
    class Mother
    {
    public:
        void tellstory(Book* b)
        {
            cout << b->getContents() << endl;
        }
        void tellstory(Paper* p)
        {
            cout << p->getContents() << endl;
        }
    };
    
    int main(int argc, char *argv[])
    {
        Book b;
        Mother m;
        Paper p;
        m.tellstory(&b);
        m.tellstory(&p);
        return 0;
    }



    #include <iostream>
    
    using namespace std;
    class iReader
    {
    public:
        virtual string getContents() = 0;//only provide a interface
    };
    class Book : public iReader
    {
    public:
        string getContents()
        {
            return "Long time ago,There is a temple in the mountain";
        }
    };
    
    class Paper : public iReader
    {
    public:
        string getContents()
        {
            return "I am handsome";
        }
    };
    
    class Mother
    {
    public:
        void tellstory(iReader * r)
        {
            cout << r->getContents() << endl;
        }
    };
    
    int main(int argc, char *argv[])
    {
        Book b;
        Mother m;
        Paper p;
        m.tellstory(&b);
        m.tellstory(&p);
        return 0;
    }
    
    
  • 相关阅读:
    Web后门工具WeBaCoo
    苹果内存取证工具volafox
    PlayMaker GUI的Normalized
    Arduino可穿戴教程之第一个程序——选择端口(三)
    缩略图信息提取工具vinetto
    jquery的defer
    toDo
    瀑布流案例
    js基本知识6
    js基本知识5
  • 原文地址:https://www.cnblogs.com/aelite/p/9813888.html
Copyright © 2011-2022 走看看