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();
            }  
  • 相关阅读:
    停止Java线程,小心interrupt()方法
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    jenkins服务器安装
    nexus私服安装
    黑客渗透测试教程
    python fabric实现远程操作和部署
    牛客网编程练习(华为机试在线训练)-----数字颠倒
    牛客网编程练习(华为机试在线训练)-----字符个数统计
    牛客网编程练习(华为机试在线训练)-----提取不重复的整数
    牛客网编程练习(华为机试在线训练)-----合并表记录
  • 原文地址:https://www.cnblogs.com/greencolor/p/1572141.html
Copyright © 2011-2022 走看看