C#基础概念二十五问

添加人:gamefriends二级(854分)   添加时间:2007-09-13    阅读次数:15793  收藏此教程
6.sealed 修饰符是干什么的?

答:

sealed 修饰符表示密封

用于类时,表示该类不能再被继承,不能和 abstract 同时使用,因为这两个修饰符在含义上互相排斥

用于方法和属性时,表示该方法或属性不能再被重写,必须和 override 关键字一起使用,因为使用 sealed 修饰符的方法或属性肯定是基类中相应的虚成员

通常用于实现第三方类库时不想被客户端继承,或用于没有必要再继承的类以防止滥用继承造成层次结构体系混乱

恰当的利用 sealed 修饰符也可以提高一定的运行效率,因为不用考虑继承类会重写该成员

示例:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace Example06
 6{
 7    class Program
 8    {
 9        class A
10        {
11            public virtual void F()
12            {
13                Console.WriteLine("A.F");
14            }

15            public virtual void G()
16            {
17                Console.WriteLine("A.G");
18            }

19        }

20        class B : A
21        {
22            public sealed override void F()
23            {
24                Console.WriteLine("B.F");
25            }

26            public override void G()
27            {
28                Console.WriteLine("B.G");
29            }

30        }

31        class C : B
32        {
33            public override void G()
34            {
35                Console.WriteLine("C.G");
36            }

37        }

38        static void Main(string[] args)
39        {
40            new A().F();
41            new A().G();
42            new B().F();
43            new B().G();
44            new C().F();
45            new C().G();
46 
47            Console.ReadLine();
48        }

49    }

50}

结果:
类 B 在继承类 A 时可以重写两个虚函数,如图所示:

由于类 B 中对 F 方法进行了密封, 类 C 在继承类 B 时只能重写一个函数,如图所示:

控制台输出结果,类 C 的方法 F 只能是输出 类B 中对该方法的实现:

A.F
A.G
B.F
B.G
B.F
C.G


7.override 和 overload 的区别?

答:

override 表示重写,用于继承类对基类中虚成员的实现

overload 表示重载,用于同一个类中同名方法不同参数(包括类型不同或个数不同)的实现

示例:

 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4 
 5namespace Example07
 6{
 7    class Program
 8    {
 9        class BaseClass
10        {
11            public virtual void F()
12            {
13                Console.WriteLine("BaseClass.F");
14            }

15        }

16        class DeriveClass : BaseClass
17        {
18            public override void F()
19            {
20                base.F();
21                Console.WriteLine("DeriveClass.F");
22            }

23            public void Add(int Left, int Right)
24            {
25                Console.WriteLine("Add for Int: {0}", Left + Right);
26            }

27            public void Add(double Left, double Right)
28            {
29                Console.WriteLine("Add for int: {0}", Left + Right);
30            }

31        }

32        static void Main(string[] args)
33        {
34            DeriveClass tmpObj = new DeriveClass();
35            tmpObj.F();
36            tmpObj.Add(1, 2);
37            tmpObj.Add(1.1, 2.2);
38 
39            Console.ReadLine();
40        }

41    }

42}

结果:
BaseClass.F
DeriveClass.F
Add for Int: 3
Add for int: 3.3


8.什么是索引指示器?

答:

实现索引指示器(indexer)的类可以象数组那样使用其实例后的对象,但与数组不同的是索引指示器的参数类型不仅限于int

简单来说,其本质就是一个含参数属性

示例:

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4 
  5namespace Example08
  6{
  7    public class Point
  8    {
  9        private double x, y;
 10        public Point(double X, double Y)
 11        {
 12            x = X;
 13            y = Y;
 14        }

 15        //重写ToString方法方便输出
 16        public override string ToString()
 17        {
 18            return String.Format("X: {0} , Y: {1}", x, y);
 19        }

 20    }

 21    public class Points
 22    {
 23        Point[] points;
 24        public Points(Point[] Points)
 25        {
 26            points = Points;
 27        }

 28        public int PointNumber
 29        {
 30            get 
 31            {
 32                return points.Length;
 33            }

 34        }
    
 35        //实现索引访问器
 36        public Point this[int Index]
 37        {
 38            get
 39            {
 40                return points[Index];
 41            }

 42        }

 43    }

 44 
 45    //感谢watson hua(http://huazhihao.cnblogs.com/)的指点
 46    //索引指示器的实质是含参属性,参数并不只限于int
 47    class WeatherOfWeek
 48    {
 49        public string this[int Index]
 50        {
 51            get
 52            {
 53                //注意case段使用return直接返回所以不需要break
 54                switch (Index)
 55                {
 56                    case 0:
 57                        {
 58                            return "Today is cloudy!";
 59                        }

 60                    case 5:
 61                        {
 62                            return "Today is thundershower!";
 63                        }

 64                    default:
 65                        {
 66                            return "Today is fine!";
 67                        }

 68                }

 69            }

 70        }

 71        public string this[string Day]
 72        {
 73            get
 74            {
 75                string TodayWeather = null;
 76                //switch的标准写法
 77                switch (Day)
 78                {
 79                    case "Sunday":
 80                        {
 81                            TodayWeather = "Today is cloudy!";
 82                            break;
 83                        }

 84                    case "Friday":
 85                        {
 86                            TodayWeather = "Today is thundershower!";
 87                            break;
 88                        }

 89                    default:
 90                        {
 91                            TodayWeather = "Today is fine!";
 92                            break;
 93                        }

 94                }

 95                return TodayWeather;
 96            }

 97        }

 98    }

 99    class Program
100    {
101        static void Main(string[] args)
102        {
103            Point[] tmpPoints = new Point[10];
104            for (int i = 0; i < tmpPoints.Length; i++)
105            {
106                tmpPoints[i] = new Point(i, Math.Sin(i));
107            }

108 
109            Points tmpObj = new Points(tmpPoints);
110            for (int i = 0; i < tmpObj.PointNumber; i++)
111            {
112                Console.WriteLine(tmpObj[i]);
113            }

114 
115 
116            string[] Week = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"};
117&nbs