日期:<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />2008-6-15<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

学习内容:接口,程序的运行过程

遗留问题:

学习总结:

 

接口与抽象类:

1.      类是对对象的抽象,可以把抽象类理解为把类当作对象,抽象成的类。接口只是一个行为的规范或规定。接口表述的是“我能做”(Can-do),抽象类更多的是定义在一系列紧密相关的类间(IS-a),而接口大多数是关系疏松但都实现某一功能的类中

2.      接口基本上不具备继承的任何具体特点,它仅仅承诺了能够调用的方法;

3.      一个类一次可以实现若干个接口,但是只能扩展一个父类。

4.      抽象类实现了OOP中的一个原则,把可变的与不可变的分离。抽象类和接口就是定义为不可变的,而把可变的由子类去实现。

5.      好的接口定义应该是具有专一功能性啊,而不是多功能的,否则造成接口污染。如果一个类只是实现了这个接口中的一个功能,而不得不去实现接口中的其他方法,就叫接口污染。

6.      如果抽象类实现接口,则可以把接口中方法映射到抽象类中作为抽象方法而不必实现,而在抽象类的子类中实现接口中的方法。

接口与抽象类:

 

1.      抽象类用于部分实现一个类,再由用户按需求对其进行不同的扩展和完善;接口只是定义一个行为的规范或规定。

2.      抽象类在组件的所有实现间提供通用的已实现功能;接口创建在大范围全异对象间使用的功能。

3.      抽象类主要用于关系密切的对象;而接口适合为不相关的类提供通用功能。

4.      抽象类主要用于设计大的功能单元;而接口用于设计小而简练的功能块。

模拟场景:教师考取驾驶执照,那么他属于教师类还是司机类呢?

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication1

{

    interface IDrivingLicenceB

    {

        void getLicence();

    }

    interface IDrivingLicenceA:IDrivingLicenceB

    {

        new void getLicence();

    }

    class Teacher : IDrivingLicenceA

    {

        public void getLicence()

        {

            Console.WriteLine(" 老师考取了A类驾驶执照" );

        }

    }

    class Student : IDrivingLicenceB

    {

        public void getLicence()

        {

            Console.WriteLine(" 学生考取了B类驾驶执照" );

        }

    }

    class Program

    {

        static void DriveCar(string name, object o)

        {

            IDrivingLicenceB dl = o as IDrivingLicenceB;

            if (dl != null)

            {

                Console.WriteLine(name + " 开动了卡车" );

            }

            else

            {

                Console.WriteLine(name + " 不能开卡车" );

            }

        }

        static void DriveBus(string name, object o)

        {

            IDrivingLicenceA dl = o as IDrivingLicenceA;

            if (dl != null)

            {

                Console.WriteLine(name + " 开动了公共汽车" );

            }

            else

            {

                Console.WriteLine(name+" 不能开公共汽车" );

            }

        }

        static void Main(string[] args)

        {

            Teacher t = new Teacher();

            DriveCar(" 教师" , t);

            DriveBus(" 教师" , t);

            Student s = new Student();

            DriveCar(" 学生" , s);

            DriveBus(" 学生" , s);

        }

    }

}

 

 

程序功能:实现IComparable接口,来实现排序

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication2

{

    class Student:IComparable// 实现系统自带的IComparable接口

    {

        private string name;

        private int age;

        public Student(string name, int age)

        {

            this.name = name;

            this.age = age;

        }

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

        public int Age

        {

            get

            {

                return age;

            }

            set

            {

                age = value;

            }

        }

        public int CompareTo(object right)

        {

            if (!(right is Student))

                throw new ArgumentException(" 参数类型必须为student类型" );

            return name.CompareTo(((Student)right).name);

        }

        public override string ToString()// 如果不重写此方法将返回的是类型

        {

            return name;

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Student[] arr =new Student[5];

            arr[0] = new Student(" 张三" , 5);

            arr[1] = new Student(" 李四" , 3);

            arr[2] = new Student(" 王五" , 4);

            arr[3] = new Student(" 赵六" , 2);

            arr[4] = new Student(" 钱七" , 1);

            Array.Sort(arr);// 调用Array类的静态方法Sort

            foreach (Student i in arr)

                Console.WriteLine(i);

        }

    }

}

 

 

 

 

 

.NET Framework ( 代码库、通用类型系统 CTS CLR)

 

    编译

C# ———— > 程序集 (.exe .dll[MSIL]) 、元信息 [ 数据信息 ] 、可选资源 [ 图片、声音 ])

             |                 |

             |                 (Microsoft Intermediate Language 微软中间语言 )

             |

             | JIT 编译 (Just-In-Time 仅在需要时才编译 MSIL)

             |

             —— > 机器代码 < ———— .NET CLR (Common Language Runtime 公共语言运行库 )

                               运行      /   \

                                       /     \

                                     托管   非托管

                                    (C#)     (C++)

 

 

总结:

 

一、用 C# .NET 兼容语言编写代码

 

              编译

二、       C# ———— > 程序集

 

              JIT 编译

三、   程序集————— > 机器代码

 

               运行

四、   托管 CLR ———— > 机器代码