C# 多态性

来源:互联网 发布:财富共赢集团 知乎 编辑:程序博客网 时间:2024/05/23 22:37

多态性以为着有多重形式。在面向对象编程范式中,多态性往往表现为“一个接口,多个功能”。

多态性可以是静态或动态的。在静态多态性中,函数的响应是在编译时发生的,在动态多态性中,函数的响应是在运行时发生的。


静态多态性

在编译时,函数和对象的链接机制被称为早起绑定,也被称为静态绑定。C#提供了两种技术来实现静态多态和动态多态性。分别为

-函数重载

-运算符重载


函数重载

在同一个范围内的相同的函数名可以定义多个,函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数的个数不同。不能重载只有返回类型不同的函数声明。

//C#静态多态性-函数重载using System;namespace PolymorphismApplication{    class PrintData    {        //函数名相同,具有不同的参数列表,参数的类型或参数的个数,不以返回值类型作为判定依据        public void print(string s)        {            Consle.WriteLine("the print value is:{0}",s);        }        public void print(int n)        {            Consle.WriteLine("the print value is:{0}",n);        }        public void print(double n)        {            Consle.WriteLine("the print value is:{0}",n);        }    }    class TestForCase    {        public static void Mian(string[] args)        {            PrintData p = new PrintData();            p.print("Hello World!")            p.print(5);            p.print(7.80);            Consle.ReadLine();        }    }}



动态多态性

C#允许使用关键字abstract创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自改抽象类的时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。

注意一些有关抽象类的规则:

-不可以创建一个抽象类的实例

-不能在抽象类外部声明一个抽象方法

-通过在类定义前面放置关键字sealed,可以将类声明为密封类。当一个类被声明为sealed时,它不能被继承。抽象类不能被声明为sealed。

下面演示了一个抽象类:

using System;namespace PolymorphismApplication{    //抽象类    abstract class Shape    {        //抽象方法,只有声明,没有方法体        public abstract int getArea();    }    class SubShape:Shape    {        private int width;        private int length;        public SubShape(int w, int l)        {            width = w;            length = l;        }        //重写父类的抽象方法,使用override关键字,在Java中不使用任何关键字,直接重写即可        public override int getArea()        {            return width*length;        }    }    class TestForCase    {        public static void Mian(string[] args)        {            SubShape s = new SubShape(5,7);            int area;            area = s.getArea();            Consle.WriteLine("Area is:{0}",area);            Consle.ReadLine();        }    }}

当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字virtual声明的。虚方法可以再不同的继承类中有不同的实现。对虚方法的调用是在运行时发生的。

动态多态性是通过抽象类和虚方法实现的。

下面的程序演示了这点:

//动态多态性-虚方法using System;namespace PolymorphismApplication{    class Shape    {        protected int width,height;        public Shape(int w,int l)        {            width = w;            length = l;        }        //虚方法        public virtual int area()        {            Consle.WriteLine("父类的面积:");            return 0;        }    }    class Rectange:Shape    {        //调用基类的构造方法        public Rectange(int a=0,int b=0):base(a,b)        {        }        //重写基类的虚方法,使用override关键字        public override int area()        {            Consle.WriteLine("Rectange 类的面积:");            return width*length;        }    }    class Triangle:Shape    {        public Triangle(int a=0, int b=0):base(a,b)        {        }        public override int area()        {            Consle.WriteLine("Triangle 类的面积:");            return width*length;        }    }    class Caller    {        public void CallArea(Shape s)        {            int a;            a = sh.area();            Console.WriteLine("面积:{0}",a);        }    }    class Test    {        public static void Mian(string[] args)        {            Caller c = new Caller();            Rectange r = new Rectange(5,6);            Triangle t = new Triangle(7,8);            c.Caller(r);            c.Caller(t);            Console.ReadLine();        }    }}




原创粉丝点击