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();
            }  
  • 相关阅读:
    hdu 1269 迷宫城堡 (并查集)
    hdu 1272 小希的迷宫 (深搜)
    hdu 1026 Ignatius and the Princess I (深搜)
    hdu 1099 Lottery
    hdu 1068 Girls and Boys (二分匹配)
    几个基础数位DP(hdu 2089,hdu 3555,uestc 1307 windy 数)
    hdu 1072 Nightmare (广搜)
    hdu 1398 Square Coins (母函数)
    hdu 1253 胜利大逃亡 (深搜)
    hdu 1115 Lifting the Stone (求重心)
  • 原文地址:https://www.cnblogs.com/greencolor/p/1572141.html
Copyright © 2011-2022 走看看