C# 中多态和重载的区别

一、多态性意味着有多重形式。

在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace PolymorphismApplication
 7 {
 8     /// <summary>
 9     /// 几何图形,抽象基类
10     /// </summary>
11     abstract class Geometric
12     {
13         public abstract double Area();
14     }
15 
16     /// <summary>
17     /// 正方形
18     /// </summary>
19     class Square : Geometric
20     {
21         private double side;
22         public Square(double a = 0)
23         {
24             side = a;
25         }
26         public override double Area()
27         {
28             Console.WriteLine("Square 类的面积:");
29             return (side * side);
30         }
31     }
32     /// <summary>
33     /// 长方形
34     /// </summary>
35     class Rectangle : Geometric
36     {
37         private double length;
38         private double width;
39         public Rectangle(double a = 0, double b = 0)
40         {
41             length = a;
42             width = b;
43         }
44         public override double Area()
45         {
46             Console.WriteLine("Rectangle 类的面积:");
47             return (width * length);
48         }
49     }
50     /// <summary>
51     /// 圆型
52     /// </summary>
53     class Circle : Geometric 
54     {
55         private double r;
56         public Circle(double a = 0)
57         {
58             r = a;
59         }
60         public override double Area()
61         {
62             Console.WriteLine("Circle 类的面积:");
63             return (3.14*r*r);
64         }
65     }
66     
67     class Program
68     {
69         static void Main(string[] args)
70         {
71             Square s = new Square(5);
72             double sa = s.Area();
73             Console.WriteLine("面积: {0}", sa);
74             Rectangle r = new Rectangle(10, 7);
75             double ra = r.Area();
76             Console.WriteLine("面积: {0}", ra);
77             Circle c = new Circle(1);
78             double ca = c.Area();
79             Console.WriteLine("面积: {0}", ca);
80             Console.ReadKey();
81         }
82     }
83 }

 

抽象基类Geometric中定义了抽象方法Area public abstract double Area(); 

不同的子类继承了基类Geometric,分别实现了Area(),但是正方形和长方形和圆型的Area()又不相同。这就是多态性。

 

 二、重载

重载是指具有相同的函数名词,但是参数类型,个数不相同。

 举个例子

编程实现两个整数相加

public static int IntPlusInt(int a, int b)
{
    return a + b;
}

 

 

没有问题,再写个double类型和double类型相加

public static double DoublePlusDouble(double a, double b)
{
    return a + b;
}

 

doub 和int相加:

public static double DoublePlusInt(double a, int b)
{
    return a + b;
}

public static double IntPlusDouble(int a, double b)
{
    return a + b;
}

 

就这样,简单的int和double类型的相加就需要4个方法。

你在计算时还得想一下,int类型相加的方法名称是什么。

重载,可以使得上面的事情变得简单:

public static int Plus(int a, int b)
{
    return a + b;
}

public static double Plus(double a, double b)
{
    return a + b;
}

public static double Plus(double a, int b)
{
    return a + b;
}

public static double Plus(int a, double b)
{
    return a + b;
}

 

方法名称全都是Plus,不需要起很多名字,方便使用。调用时,vs会根据变量类型自动匹配到相应的方法。

 

附全部代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Overload
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.WriteLine("1+2=" + IntPlusInt(1, 2));
13             Console.WriteLine("1.1+2.2=" + DoublePlusDouble(1.1, 2.2));
14             Console.WriteLine("1.1+2=" + DoublePlusInt(1.1, 2));
15             Console.WriteLine("1+2.2=" + IntPlusDouble(1, 2.2));
16 
17 
18             Console.WriteLine("1+2=" + Plus(1, 2));
19             Console.WriteLine("1.1+2.2=" + Plus(1.1, 2.2));
20             Console.WriteLine("1.1+2=" + Plus(1.1, 2));
21             Console.WriteLine("1+2.2=" + Plus(1, 2.2));
22 
23             Console.ReadKey();
24         }
25 
26         #region 不使用重载
27         public static int IntPlusInt(int a, int b)
28         {
29             return a + b;
30         }
31 
32         public static double DoublePlusDouble(double a, double b)
33         {
34             return a + b;
35         }
36 
37         public static double DoublePlusInt(double a, int b)
38         {
39             return a + b;
40         }
41 
42         public static double IntPlusDouble(int a, double b)
43         {
44             return a + b;
45         }
46         #endregion
47 
48         #region 使用重载
49         public static int Plus(int a, int b)
50         {
51             return a + b;
52         }
53 
54         public static double Plus(double a, double b)
55         {
56             return a + b;
57         }
58 
59         public static double Plus(double a, int b)
60         {
61             return a + b;
62         }
63 
64         public static double Plus(int a, double b)
65         {
66             return a + b;
67         }
68         #endregion
69     }
70 }
View Code

 

posted @ 2018-06-21 14:12  自恋狂学长  阅读(1822)  评论(0编辑  收藏  举报