C# 多态性(Polymorphism)全面详解
                           
天天向上
发布: 2025-04-13 13:48:49

原创
43 人浏览过

下面是对 C# 多态性(Polymorphism) 的更全面、系统、深入的解释,涵盖原理、分类、语法机制、实战应用、注意事项以及最佳实践,辅以高质量官方文档和示例。


1. 什么是多态(Polymorphism)?

“多态”来自希腊语,意为“多种形态”。在面向对象编程中,它指的是使用同一个方法名或接口,对不同的对象产生不同的行为。

📌 通俗理解:同一个方法调用,在不同的类上执行,产生不同的结果。


2. 多态的两种形式

类型特点实现方式示例
编译时多态(静态)编译期间确定调用方法重载、运算符重载Add(int, int) vs Add(double, double)
运行时多态(动态)程序运行时决定调用哪一个实现虚方法 + 重写、接口实现父类变量指向子类对象调用被重写的方法

3. 编译时多态(静态多态)

3.1 方法重载(Method Overloading)

在同一个类中,允许方法名相同,但参数类型、个数、顺序不同。

class Printer {
    public void Print(string text) => Console.WriteLine(text);
    public void Print(int number) => Console.WriteLine(number);
}

✅ 编译器在编译期间根据传入参数决定调用哪个方法。

3.2 运算符重载(Operator Overloading)

可以定义如何对自定义类型使用 +, -, *, / 等运算符。

class Vector {
    public int X, Y;
    public static Vector operator +(Vector v1, Vector v2)
        => new Vector { X = v1.X + v2.X, Y = v1.Y + v2.Y };
}

4. 运行时多态(动态多态)

通过 虚方法(virtual方法重写(override 实现。

4.1 使用虚方法与重写

class Animal {
    public virtual void Speak() => Console.WriteLine("Animal speaks");
}

class Dog : Animal {
    public override void Speak() => Console.WriteLine("Dog barks");
}

4.2 多态性演示

Animal myPet = new Dog();
myPet.Speak();  // 输出: Dog barks

✔️ 尽管变量是 Animal 类型,但调用的是 Dog 类中的 Speak 方法,这是运行时多态性。


5. 接口多态(Interface-based Polymorphism)

接口是实现多态性的另一种重要方式。

interface IShape {
    void Draw();
}

class Circle : IShape {
    public void Draw() => Console.WriteLine("Drawing Circle");
}

class Square : IShape {
    public void Draw() => Console.WriteLine("Drawing Square");
}

使用接口多态

IShape shape = new Circle();
shape.Draw();  // 输出 Drawing Circle

6. 抽象类与多态(Abstract Class)

抽象类中可以定义抽象方法,要求派生类必须实现。

abstract class Shape {
    public abstract void Draw();
}

class Triangle : Shape {
    public override void Draw() => Console.WriteLine("Draw triangle");
}
Shape s = new Triangle();
s.Draw(); // 输出 Draw triangle

7. 多态的应用场景

场景多态作用
GUI 控件系统所有控件继承自同一基类,重写绘制行为
日志系统抽象日志写入,支持写入文件、数据库等多种方式
数据访问层使用接口来统一不同数据库的操作(如 MySQL、SQL Server)
游戏开发敌人、NPC 都继承自 Entity 类,调用统一方法处理行为

8. 注意事项

说明
new 隐藏使用 new 关键字可以隐藏基类方法,但不是真正的重写
非虚方法不能重写未标记为 virtualabstract 的方法
多态绑定只有通过基类或接口引用调用方法时才体现出多态性
public class A {
    public virtual void Show() => Console.WriteLine("A");
}

public class B : A {
    public new void Show() => Console.WriteLine("B");
}

A obj = new B();
obj.Show(); // 输出 A(不是 B)

9. 官方文档与权威链接


10. 多态设计模式实例

策略模式(Strategy Pattern)

interface ICompressionStrategy {
    void Compress(string fileName);
}

class ZipCompression : ICompressionStrategy {
    public void Compress(string fileName) => Console.WriteLine("ZIP Compression");
}

class RarCompression : ICompressionStrategy {
    public void Compress(string fileName) => Console.WriteLine("RAR Compression");
}

class Compressor {
    private ICompressionStrategy _strategy;
    public Compressor(ICompressionStrategy strategy) => _strategy = strategy;
    public void Compress(string fileName) => _strategy.Compress(fileName);
}

使用:

var compressor = new Compressor(new ZipCompression());
compressor.Compress("file.txt");  // 输出: ZIP Compression

总结

分类编译时多态运行时多态
实现方式重载/重载运算符虚函数 + 重写
判断时间编译时运行时
优点简单、高效灵活、可扩展
代表关键词overload, operatorvirtual, override, interface, abstract

更多详细内容,请关注其他相关文章!

发表回复 0

Your email address will not be published. Required fields are marked *