zoukankan      html  css  js  c++  java
  • Interface

    A simple example for interface

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        class Factory
        {
            public static ITest create(int itype) // return a ITest(interface) Type. This interface can be used to save the return method.
            {
                if (itype == 1)
                {
                    return new Test();
                }
                else if (itype == 2)
                {
                    return new Test2();
                }
                else if (itype == 3)
                {
                    return new Test3();
                }
                else
                {
                    return new Test4();
                }
            }
        }
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        interface ITest
        {
            // This interface class collects the same method(s) from Test.cs, Test2.cs, Test3.cs and Test4.cs.
            // So that we can modify these classes through modify the methods in these classes.
            string iText();
            //  string iText2();
        }
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        class Test : ITest
        {
            #region ITest Members

            public string iText()
            {
                // TODO:  Add Test.printText implementation
                return ("Test string.");
            }
            //public void testmethod()
            //{
            //    int aaa;
            //}
            #endregion
        }
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        class Test2 : ITest
        {
            #region ITest Members

            public string iText()
            {
                // TODO:  Add Test.printText implementation
                return ("Test2 string.");
            }
            //public void testmethod1()
            //{
            //    int aaa;
            //}

            #endregion

        }
    }



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        class Test3 : ITest
        {
            #region ITest Members

            public string iText()
            {
                // TODO:  Add Test.printText implementation
                return ("Test3 string.");
            }
           
            #endregion

        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace WindowsFormsApplication4
    {
        class Test4 : ITest
        {
            #region ITest Members

            public string iText()
            {
                // TODO:  Add Test.printText implementation
                return ("Test4 string.");
            }
            //public void testmethod4()
            //{
            //    int aaa;
            //}
            #endregion
        }
    }


         private void button1_Click(object sender, EventArgs e)
            {
                ITest it; // We define a interface function it.
                it = Factory.create(4); // Return a interface function, after Factory.create.
                this.label1.Text = it.iText(); // Directly use the interface function "it", just like Test4.iText();
            }  
  • 相关阅读:
    01 drf源码剖析之restful规范
    08 Flask源码剖析之flask拓展点
    07 flask源码剖析之用户请求过来流程
    06 flask源码剖析之路由加载
    05 flask源码剖析之配置加载
    04 flask源码剖析之LocalStack和Local对象实现栈的管理
    03 flask源码剖析之threading.local和高级
    02 flask源码剖析之flask快速使用
    01 flask源码剖析之werkzurg 了解wsgi
    MVC之Filter
  • 原文地址:https://www.cnblogs.com/greencolor/p/1572141.html
Copyright © 2011-2022 走看看