首页 > 代码库 > 接口数组

接口数组

using System;using System.Collections.Generic;using System.Text;namespace myTest{    class Program    {        static void Main(string[] args)        {            MyClass_1 myClass_11 = new MyClass_1();            MyClass_1 myClass_12 = new MyClass_1();            MyClass_2 myClass_21 = new MyClass_2();            MyClass_2 myClass_22 = new MyClass_2();            MyInterface[] classes = { myClass_11, myClass_12, myClass_21, myClass_22 };            foreach (MyInterface myClass in classes)            {                myClass.MyMethod();            }            Console.ReadLine();        }    }    interface MyInterface    {        void MyMethod();        int MyProperty        {            get;            set;        }    }    class MyClass_1 : MyInterface    {        private int myField;        public MyClass_1()        {            Console.WriteLine("测试类_1。");        }        public void MyMethod()        {            Console.WriteLine("自定义方法_1");        }        public int MyProperty        {            get            {                return myField;            }            set            {                myField = value;            }        }    }    class MyClass_2 : MyInterface    {        private int myField;        public MyClass_2()        {            Console.WriteLine("测试类_2。");        }        public void MyMethod()        {            Console.WriteLine("自定义方法_2");        }        public int MyProperty        {            get            {                return myField;            }            set            {                myField = value;            }        }    }}/*运行结果:测试类_1。测试类_1。测试类_2。测试类_2。自定义方法_1自定义方法_1自定义方法_2自定义方法_2 */