接口 interface
接口
多重接口
在C#中,只要不发生命名冲突,就完全可以允许多重接口实现。
显示接口
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 }
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 }
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 }
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 }