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

接口 interface

接口
 1public interface IPict  //隐式声明为public
 2    {
 3        int DelectImage();
 4        void DisplayImage();
 5    }

 6    public class MyImage:IPict  //不是继承,是实现于IPict
 7    {
 8        public int DelectImage()
 9        {
10            Console.WriteLine("DelectImage实现!");
11            return (5);
12        }

13
14        public void DisplayImage()
15        {
16            Console.WriteLine("DisplayImage实现!");
17        }

18        static void Main(string[] args)
19        {
20            MyImage objM = new MyImage();
21            objM.DisplayImage();
22            int t = objM.DelectImage();
23            Console.WriteLine(t);
24        }

25    }

多重接口
在C#中,只要不发生命名冲突,就完全可以允许多重接口实现。
 1 public interface IPictManip
 2    {
 3        void ApplyAlpha();
 4    }

 5    public interface IPict
 6    {
 7        int DelectImage();
 8        void DisplayImage();
 9    }

10    public class BaselO
11    {
12        public void Open()
13        {
14            Console.WriteLine("BaselO的Open方法");
15        }

16    }

17    public class MyImage : BaselO, IPict, IPictManip
18    {
19        public int DelectImage()
20        {
21            Console.WriteLine("DelectImage实现!");
22            return (5);
23        }

24
25        public void DisplayImage()
26        {
27            Console.WriteLine("DisplayImage实现!");
28        }

29
30        public void ApplyAlpha()
31        {
32            Console.WriteLine("ApplyAlpha实现!");
33        }

34        static void Main(string[] args)
35        {
36            MyImage objm = new MyImage();
37            objm.Open();
38            objm.ApplyAlpha();
39            objm.DelectImage();
40            objm.DisplayImage();
41          
42        }

43    }

显示接口
 1public interface IPictManip
 2    {
 3        void ApplyAlpha();
 4    }

 5    public interface IPict
 6    {
 7        void ApplyAlpha();
 8    }

 9    public class BaselO
10    {
11        public void Open()
12        {
13            Console.WriteLine("BaselO的Open方法");
14        }

15    }

16    public class MyImage : BaselO, IPict, IPictManip
17    {
18        void IPict.ApplyAlpha()
19        {
20            Console.WriteLine("实现了IPict的ApplyAlpha方法");
21        }

22        void IPictManip.ApplyAlpha()
23        {
24            Console.WriteLine("实现了IPictManip的ApplyAlpha方法");
25        }

26        static void Main(string[] args)
27        {
28            MyImage objm = new MyImage();
29            objm.Open();
30            IPict Pict = objm;  //IPict引用
31            Pict.ApplyAlpha();
32            IPictManip PcitManip = objm;
33            PcitManip.ApplyAlpha();
34        }

35    }
posted @ 2007-12-04 13:07  吴有鋆  阅读(306)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3