C#基础概念二十五问

添加人:gamefriends二级(855分)   添加时间:2007-09-13    阅读次数:15795  收藏此教程
17.接口的多继承会带来哪些问题?

答:

C# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 C# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明

示例:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace Example17
 6{
 7    class Program
 8    {
 9        //一个完整的接口声明示例
10        interface IExample
11        {
12            //属性
13            string P
14            {
15                get;
16                set;
17            }

18            //方法
19            string F(int Value);
20            //事件
21            event EventHandler E;
22            //索引指示器
23            string this[int Index]
24            {
25                get;
26                set;
27            }

28        }

29        interface IA
30        {
31            int Count { get; set;}
32        }

33        interface IB
34        {
35            int Count();
36        }

37        //IC接口从IA和IB多重继承
38        interface IC : IA, IB
39        {
40        }

41        class C : IC
42        {
43            private int count = 100;
44            //显式声明实现IA接口中的Count属性
45            int IA.Count
46            {
47                get { return 100; }
48                set { count = value; }
49            }

50            //显式声明实现IB接口中的Count方法
51            int IB.Count()
52            {
53                return count * count;
54            }

55        }

56        static void Main(string[] args)
57        {
58            C tmpObj = new C();
59 
60            //调用时也要显式转换
61            Console.WriteLine("Count property: {0}", ((IA)tmpObj).Count);
62            Console.WriteLine("Count function: {0}", ((IB)tmpObj).Count());
63 
64            Console.ReadLine();
65        }

66    }

67}

结果:
Count property: 100
Count function: 10000


18.抽象类和接口的区别?

答:

抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义

抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性

分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”

为外部提供调用或功能需要扩充时优先使用接口


19.别名指示符是什么?

答:

通过别名指示符我们可以为某个类型起一个别名

主要用于解决两个命名空间内有同名类型的冲突或避免使用冗余的命名空间

别名指示符在所有命名空间最外层定义,作用域为整个单元文件。如果定义在某个命名空间内,那么它只在直接隶属的命名空间内起作用

示例:

Class1.cs:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01
 6{
 7    class Class1
 8    {
 9        public override string ToString()
10        {
11            return "com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1";
12        }

13    }

14}

Class2.cs:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02
 6{
 7    class Class1
 8    {
 9        public override string ToString()
10        {
11            return "com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02's Class1";
12        }

13    }

14}

主单元(Program.cs):

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5 
 6//使用别名指示符解决同名类型的冲突
 7//在所有命名空间最外层定义,作用域为整个单元文件
 8using Lib01Class1 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
 9using Lib02Class2 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02.Class1;
10 
11namespace Example19
12{
13    namespace Test1
14    {
15        //Test1Class1在Test1命名空间内定义,作用域仅在Test1之内
16        using Test1Class1 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
17 
18        class Class1
19        {
20            //Lib01Class1和Lib02Class2在这可以正常使用
21            Lib01Class1 tmpObj1 = new Lib01Class1();
22            Lib02Class2 tmpObj2 = new Lib02Class2();
23            //TestClass1在这可以正常使用
24            Test1Class1 tmpObj3 = new Test1Class1();
25        }

26    }

27    namespace Test2
28    {
29        using Test1Class2 = com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;
30 
31        class Program
32        {
33            static void Main(string[] args)
34            {
35                //Lib01Class1和Lib02Class2在这可以正常使用
36                Lib01Class1 tmpObj1 = new Lib01Class1();
37                Lib02Class2 tmpObj2 = new Lib02Class2();
38 
39                //注意这里,TestClass1在这不可以正常使用。
40                //因为,在Test2命名空间内不能使用Test1命名空间定义的别名
41                //Test1Class1 tmpObj3 = new Test1Class1();
42                
43                //TestClass2在这可以正常使用
44                Test1Class2 tmpObj3 = new Test1Class2();
45 
46                Console.WriteLine(tmpObj1);
47                Console.WriteLine(tmpObj2);
48                Console.WriteLine(tmpObj3);
49 
50                Console.ReadLine();
51            }

52        }

53    }

54}

结果:

com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1
com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02's Class1
com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01's Class1
9页 第6上一页123456789下一页
相关的教程: CSharp
收藏此教程

当前平均分: 5.0(1 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: