本帖最后由 kj289907795 于 2010-10-13 12:57:44 编辑

解决方案 »

  1.   

    打个比方,有一个风尘女,每晚接客,客人就是一个接口:public interface IWhoremaster
    {
        Fck(); // 此处省略一个元音字母
        Pay();
    }风尘女只在乎来的人是不是实现了 IWhoremaster 接口,不在乎他具体是哪个人,于是我们说,这个风尘女是在针对接口编程
      

  2.   

    你的意思是不是一开始实例化A对象,调用A的do()方法,然后需求改变,需要实例化B对象,调用B的do()方法?
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace interfaceoo
    {
        interface myinterface
        {
            string STR
            {
                get;
                set;
            }
            void outMethod();
        }
            class interfaceuse : myinterface
            {
                string str = "接口实现";
                public string STR
                {
                    get { return str; }
                    set { str = value; }
                }
                public void outMethod()
                {
                    Console.WriteLine(this.STR);
                }
            }        class Program
            {
                static void Main(string[] args)
                {
                    interfaceuse interfaceuser = new interfaceuse();
                    interfaceuser.outMethod();
                }
            }
    }
    实现了 上述接口,有什么作用呢?
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace interfaceoo
    {
                class interfaceuse 
            {
                string str = "接口实现";
                public string STR
                {
                    get { return str; }
                    set { str = value; }
                }
                public void outMethod()
                {
                    Console.WriteLine(this.STR);
                }
            }        class Program
            {
                static void Main(string[] args)
                {
                    interfaceuse interfaceuser = new interfaceuse();
                    interfaceuser.outMethod();
                }
            }
    }
    改成这样  结果是一样疑惑
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace interfaceoo
    {
        interface D
        {
            string STR
            {
                get;
                set;
            }
            void outMethod();
        }
        class A : D
        {
            string str = "接口实现";
            public string STR
            {
                get { return str + "A"; }
                set { str = value; }
            }
            public void outMethod()
            {
                if (Console.ReadLine().ToString() == "1")
                    Console.WriteLine(this.STR);
            }
        }
        class B : D
        {
            string str = "接口实现";
            public string STR
            {
                get { return str + "B"; }
                set { str = value; }
            }
            public void outMethod()
            {
                if (Console.ReadLine().ToString() == "2")
                Console.WriteLine(this.STR);
            }
        }
        class C : D
        {
            string str = "接口实现";
            public string STR
            {
                get { return str + "C"; }
                set { str = value; }
            }
            public void outMethod()
            {
                if(Console.ReadLine().ToString() == "3")
                Console.WriteLine(this.STR);
                Console.Read();
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                //开始的业务需要用A的实现
                  //你可以写
                A a=new A();D d=a as D;test(d);
             // 当你的业务需要用B实现时你只要改成 B b=new B();D d=b as D;test(d);
             //就可以了不需要改动test()方法
               //    ''''''''''''''''''''''
                //        ''''''''''''''''''''''''''        }
            public test(D d);
            {
            //    ''''''''''''''''''''''           //这里你具体的业务需求代码
                d.outMethod();
             //    ''''''''''''''''''''''        }
        }
    }
      

  5.   

    针对接口编程,是咱们做程序设计的基本国策。
    接口定义的是规范,这是它类Class在概念上的本质区别,当我们谈接口的时候,谈的是一个事物应该具备的动作和属性,而谈到类的时候,就已经具体到了事物。
      

  6.   

    A a = new A(); D d = a as D; d.outMethod();
    可以实现 类A 
      

  7.   

    换一种写法可能更好理解 class Program
        {
            static void Main(string[] args)
            {
                test((new A()).GetType().ToString());
                test((new B()).GetType().ToString());
                test((new C()).GetType().ToString());
            }
            public test(string type);
            {
            
               D d = System.Activator.CreateInstance(System.Type.GetType(type)) as D;
               D.outMethod();
            }
        }
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace interfaceoo
    {
                class interfaceuse 
            {
                string str = "接口实现";
                public string STR
                {
                    get { return str; }
                    set { str = value; }
                }
                public void outMethod()
                {
                    Console.WriteLine(this.STR);
                }
            }        class Program
            {
                static void Main(string[] args)
                {
                    interfaceuse interfaceuser = new interfaceuse();
                    interfaceuser.outMethod();
                }
            }
    }改成这样 结果是一样疑惑 
     
    这个就OK的!
      

  9.   

    多态明白了
    接口也就明白了
    若干设计模式也就明白了所以 请好好认识多态<大话设计模式> 后面一些附录