如题

解决方案 »

  1.   

    参考
    下面的示例演示了接口实现。在此例中,接口 IPoint 包含属性声明,后者负责设置和获取字段的值。Point 类包含属性实现。
    // keyword_interface_2.cs
    // Interface implementation
    using System;
    interface IPoint
    {
       // Property signatures:
       int x
       {
          get;
          set;
       }   int y
       {
          get;
          set;
       }
    }class Point : IPoint
    {
       // Fields:
       private int _x;
       private int _y;   // Constructor:
       public Point(int x, int y)
       {
          _x = x;
          _y = y;
       }   // Property implementation:
       public int x
       {
          get
          {
             return _x;
          }      set
          {
             _x = value;
          }
       }   public int y
       {
          get
          {
             return _y;
          }
          set
          {
             _y = value;
          }
       }
    }class MainClass
    {
       static void PrintPoint(IPoint p)
       {
          Console.WriteLine("x={0}, y={1}", p.x, p.y);
       }   static void Main()
       {
          Point p = new Point(2, 3);
          Console.Write("My Point: ");
          PrintPoint(p);
       }
    }
      

  2.   

    using System;
    interface IPoint
    {
       // Property signatures:
       int x
       {
          get;
          set;
       }   int y
       {
          get;
          set;
       }
    }class Point : IPoint
    {
       // Fields:
       private int _x;
       private int _y;   // Constructor:
       public Point(int x, int y)
       {
          _x = x;
          _y = y;
       }   // Property implementation:
       public int x
       {
          get
          {
             return _x;
          }      set
          {
             _x = value;
          }
       }   public int y
       {
          get
          {
             return _y;
          }
          set
          {
             _y = value;
          }
       }
    }class MainClass
    {
       static void PrintPoint(IPoint p)
       {
          Console.WriteLine("x={0}, y={1}", p.x, p.y);
       }   static void Main()
       {
          Point p = new Point(2, 3);
          Console.Write("My Point: ");
          PrintPoint(p);
       }
    }
      

  3.   

    using System;interface I1
    {
      void do();
    }
    class class1:I1
    {
       public void do()
       {
          Console.WriteLine("我是第一个人");
       }
    }
    class class2:I1
    {
       public void do()
       {
          Console.WriteLine("第二");
       }
    }class MainClass 

      static void PrintPoint(IPoint p) 
      { 
          Console.WriteLine("我有什么用?"); 
      }   static void Main() 
      { 
         I1 obj_I1  =new class1();
         obj_I1.do();
         obj_I1 = new class2();
         obj_I1.do();
      } 
      

  4.   

    class MainClass 

      static void PrintPoint(IPoint p) 
      { 
          Console.WriteLine("我有什么用?"); 
      } 
    // 是引用楼上 几层那 代码。忽略吧
      

  5.   

    接口就是主机上的usb插槽,你能插鼠标也可以插U盘还可以插mpn,手机,摄像头......
      

  6.   

    一句话:接口就是为了减少变化对程序的影响。interface IExecutor{
    Execute();
    }这个执行器接口可以实现为:
    class SqlExecutor:IExecutor
    或者
    class FileExecutor:IExecutor他们都能执行,但是可能一个事执行SQL另一个则是执行文件。对于框架设计来说这点很重要,框架只需要知道我需要执行,但是到底怎么执行则有实现者来提供。
      

  7.   

    接口就是,我用的人 必须会 干接口规定的事情,如会上CSDN
     
      

  8.   

    来看一个使用接口的实例,创建一个接口Icipher,用来指定一个加密和字符串的方法。接口定义:view plaincopy to clipboardprint?
    namespace Cipher   
    {   
        public interface ICipher   
        {   
            //加密   
            string encode(string str);   
            //解密   
            string decode(string str);   
        }   
    }  
    namespace Cipher
    {
        public interface ICipher
        {
            //加密
            string encode(string str);
            //解密
            string decode(string str);
        }
    }下面两个类实现了Icipher。view plaincopy to clipboardprint?
    /*简单的易位代码,通过将每个字符都向高位一定一位来加密字符串*/  
        public class SimpleCipher:ICipher   
        {   
            public string encode(string str)   
            {   
                string ciphertext = "";   
                for (int i = 0; i < str.Length; ++i)   
                {   
                    Console.WriteLine(i.ToString()+"    "+(str[i] + 1).ToString());   
                    ciphertext = ciphertext + (char)(str[i] + 1);   
                }   
                return ciphertext;   
            }   
            public string decode(string str)   
            {   
                string plaintext = "";   
                for (int i = 0; i < str.Length; ++i)   
                {                   
                    plaintext = plaintext + (char)(str[i] - 1);   
                }   
                return plaintext;   
            }  
    /*简单的易位代码,通过将每个字符都向高位一定一位来加密字符串*/
        public class SimpleCipher:ICipher
        {
            public string encode(string str)
            {
                string ciphertext = "";
                for (int i = 0; i < str.Length; ++i)
                {
                    Console.WriteLine(i.ToString()+"    "+(str[i] + 1).ToString());
                    ciphertext = ciphertext + (char)(str[i] + 1);
                }
                return ciphertext;
            }
            public string decode(string str)
            {
                string plaintext = "";
                for (int i = 0; i < str.Length; ++i)
                {                
                    plaintext = plaintext + (char)(str[i] - 1);
                }
                return plaintext;
            } view plaincopy to clipboardprint?
    /*通过将每个字符与一个16位的密钥进行XOR(异或)运算加密字符串*/  
        public class BitCipher : ICipher   
        {   
            ushort key;   
            public BitCipher(ushort k)   
            {   
                key = k;   
            }   
            public string encode(string str)   
            {   
                string ciphertext = "";   
                for (int i = 0; i < str.Length; ++i)   
                {   
                    ciphertext = ciphertext + (char)(str[i] ^ key);   
                }   
                return ciphertext;   
            }   
            public string decode(string str)   
            {   
                string plaintext = "";   
                for (int i = 0; i < str.Length; ++i)   
                {   
                    plaintext = plaintext + (char)(str[i] ^ key);   
                }   
                return plaintext;   
            }  
    /*通过将每个字符与一个16位的密钥进行XOR(异或)运算加密字符串*/
        public class BitCipher : ICipher
        {
            ushort key;
            public BitCipher(ushort k)
            {
                key = k;
            }
            public string encode(string str)
            {
                string ciphertext = "";
                for (int i = 0; i < str.Length; ++i)
                {
                    ciphertext = ciphertext + (char)(str[i] ^ key);
                }
                return ciphertext;
            }
            public string decode(string str)
            {
                string plaintext = "";
                for (int i = 0; i < str.Length; ++i)
                {
                    plaintext = plaintext + (char)(str[i] ^ key);
                }
                return plaintext;
            } 虽然SimpleCipher和BitCipher在实现上完全不同,但他们都实现了ICipher接口。下面示范了两个类的用法:view plaincopy to clipboardprint?
    static void Main(string[] args)   
            {   
                ICipher ciphRef;   
                BitCipher bit = new BitCipher(27);   
                SimpleCipher sc = new SimpleCipher();   
      
                string plain;   
                string coded;   
      
                ciphRef = sc;   
                Console.WriteLine("Using simple cipher.");   
                plain = "testing";   
                coded = ciphRef.encode(plain);   
                Console.WriteLine("Cipher text:"+coded);   
      
                plain = ciphRef.decode(coded);   
                Console.WriteLine("Plain text:"+plain);   
      
      
      
      
                ciphRef = bit;   
                Console.WriteLine("\nUsing bitwise cipher.");   
                plain = "testing";   
                coded = ciphRef.encode(plain);   
                Console.WriteLine("Cipher text:"+coded);   
                plain = ciphRef.decode(coded);   
                Console.WriteLine("Plain text:"+plain);   
            }  
    static void Main(string[] args)
            {
                ICipher ciphRef;
                BitCipher bit = new BitCipher(27);
                SimpleCipher sc = new SimpleCipher();            string plain;
                string coded;            ciphRef = sc;
                Console.WriteLine("Using simple cipher.");
                plain = "testing";
                coded = ciphRef.encode(plain);
                Console.WriteLine("Cipher text:"+coded);            plain = ciphRef.decode(coded);
                Console.WriteLine("Plain text:"+plain);
                ciphRef = bit;
                Console.WriteLine("\nUsing bitwise cipher.");
                plain = "testing";
                coded = ciphRef.encode(plain);
                Console.WriteLine("Cipher text:"+coded);
                plain = ciphRef.decode(coded);
                Console.WriteLine("Plain text:"+plain);
            } 创建加密接口的好处在于,无论加密方法如何实现,只要类中实现了该接口,就可以以相同的方式访问它。
    我的博客:http://blog.csdn.net/kdmhh/archive/2009/08/23/4474387.aspx