接口作为参数传递、返回


接口做为参数传递,传递的是实现了接口的对象;

接口作为类型返回,返回的是实现了接口的对象。

 http://blog.csdn.net/tuziblog/article/details/4553922

接口的传递与返回就是围绕着上面的两句话展开的。

 

分别写两个例子来说明:

 

接口做为参数传递 

 

[c-sharp]  view plain copy
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            Program p = new Program();  
  6.            Man m = new Man();         //这里,想实现谁接口里的方法,就实例化谁,然后在下边就传谁  
  7.            //Woman w = new Woman();  
  8.            p.Get(w);                  //传递的是实现了接口的对象            
  9.            Console.ReadLine();  
  10.        }  
  11.        public void Get(IPerson ipn)   //接口做为参数传递  
  12.        {  
  13.            ipn.Say();  
  14.        }  
  15.    }  
  16.    /// <summary>  
  17.    /// 一个人类的接口  
  18.    /// </summary>  
  19.    public interface IPerson   
  20.    {  
  21.        void Say();  
  22.    }  
  23.    /// <summary>  
  24.    /// 一个男人的类  
  25.    /// </summary>  
  26.    public class Man : IPerson  
  27.    {       
  28.        public void Say()  
  29.        {  
  30.            Console.WriteLine("我是一个男人");  
  31.        }  
  32.    }  
  33.    /// <summary>  
  34.    /// 一个女人的类  
  35.    /// </summary>  
  36.    public class Woman : IPerson  
  37.    {  
  38.        public void Say()  
  39.        {  
  40.            Console.WriteLine("我是一个女人");  
  41.        }  
  42.    }  

 

 

接口做为参数返回 

 

[c-sharp]  view plain copy
  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           string a = Console.ReadLine();  
  6.           Program pr = new Program();  
  7.           pr.Getsay(a);  
  8.           Console.ReadLine();  
  9.       }  
  10.       public Person Getsay(string a)   
  11.       {  
  12.           Person p = null;  
  13.           switch (a)   
  14.           {  
  15.               case "1":  
  16.                   p = new Man();          
  17.                   p.Say();  
  18.                   break;  
  19.               case "2":  
  20.                   p = new Woman();  
  21.                   p.Say();  
  22.                   break;  
  23.           }  
  24.           return p;  
  25.       }  
  26.   }  
  27.   public interface Person   
  28.   {  
  29.       void Say();  
  30.   }  
  31.   public class Man : Person  
  32.   {  
  33.       public void Say()  
  34.       {  
  35.           Console.WriteLine("我是男人");  
  36.       }  
  37.   }  
  38.   public class Woman : Person  
  39.   {  
  40.       public void Say()  
  41.       {  
  42.           Console.WriteLine("我是女人");  
  43.       }  
  44.   }  

 

接口做为参数返回其实就是多态。

 

抽象类的传递和接口一样。不再说了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值