• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
冥海
博客园    首页    新随笔    联系   管理    订阅  订阅

我看接口(二)

1.声明一个接口
interface Comp
{
    bool Comp1(int i, int j);
}

2.实现接口
class Incomp : Comp
{
   public bool Comp1(int i, int j)
   {
            return i > j;

   }
   
}


3.委托传递方法名,接口传递实现接口的类名
static void Main(string[] args)
{
       List<int> list1 = new List<int>();
       list1.Add(10);
       list1.Add(20);
       list1.Add(30);
       list1.Add(-15);

       int j = list2(list1, new Incomp());//实例化一个类,把实现接口的类得对象传递过去
       Console.WriteLine(j);
       Console.ReadKey();

}

4.
static int list2(List<int> list1, Comp c)//接口的对象来接收实现接口的类得对象。
{
         int max = list1[0];
         List<int> list2 = new List<int>();
         foreach (int i in list1)
         {
            if (c.Comp1(max,i)) 接口的对象.接口实现的方法
            {
              max = i;
            }

         }
         return max;

}
       

posted @ 2011-04-18 11:05  冥海  阅读(98)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3