目录

一,编译时多态

1,方法重载

2,运算符重载

二,运行时多态

1,方法重写

2,接口实现


多态允许对象在不同的上下文中表现出不同的行为,简单来说,多态是指不同的类可以通过相同的接口来执行不同的操作,c#中的多态主要分为两种类型:

1,编译时多态(静态多态):主要通过方法重载和运算符重载实现

2,运行时多态(动态多态):主要通过方法重写(virtual和override)和接口实现

一,编译时多态

编译时多态主要依赖于方法重载和运算符重载

1,方法重载

方法重载是指在同一个类中,可以定义多个同名的方法,但他们的参数不同,编译 器根据调用时传入的参数来决定调用那个方法

例:

using System;

public class Calculator

{
    // 方法重载
    public int Add(int a, int b)
    {
        return a + b;
    }

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

    public string Add(string a, string b)
    {
        return a + b;
    }
}

class Program
{
    static void Main()
    {
        Calculator calculator = new Calculator();

        Console.WriteLine(calculator.Add(1, 2));          // 输出: 3

        Console.WriteLine(calculator.Add(1.5, 2.5));      // 输出: 4

        Console.WriteLine(calculator.Add("Hello", " World!"));  // 输出: Hello World!

    }

}

在这个例子中,Add()方法根据参数的不同类型表现出不同的行为

2,运算符重载

可以通过对运算法重载自定义运算符的行为,这里以重载“+”运算符为例:

using System;



public class Point
{

    public int X { get; set; }

    public int Y { get; set; }



    // 重载加法运算符

    public static Point operator +(Point p1, Point p2)
    {

        return new Point { X = p1.X + p2.X, Y = p1.Y + p2.Y };

    }

}



class Program
{

    static void Main()
    {

        Point p1 = new Point { X = 1, Y = 2 };

        Point p2 = new Point { X = 3, Y = 4 };

        Point p3 = p1 + p2;



        Console.WriteLine($"p3.X = {p3.X}, p3.Y = {p3.Y}");  // 输出: p3.X = 4, p3.Y = 6

    }

}

这里重载了加法运算符,使得可以直接对Point类型的对象进行加法运算

二,运行时多态

运行时多态是c#常用的多态形式,通常通过方法重写和接口来实现

1,方法重写

方法重写允许子类重新定义从父类继承的方法,当在基类声明一个方法为virtual(虚方法),在派生类中使用override来重写该方法

例:

using System;



public class Animal

{

    public virtual void MakeSound()
    {

        Console.WriteLine("Animal makes a sound");

    }

}



public class Dog : Animal
{

    // 重写父类的 MakeSound 方法

    public override void MakeSound()
    {

        Console.WriteLine("Dog barks");

    }

}



public class Cat : Animal
{

    // 重写父类的 MakeSound 方法

    public override void MakeSound()
    {

        Console.WriteLine("Cat meows");

    }

}



class Program
{

    static void Main()
    {

        Animal myAnimal = new Animal();

        Animal myDog = new Dog();

        Animal myCat = new Cat();



        myAnimal.MakeSound();  // 输出: Animal makes a sound

        myDog.MakeSound();     // 输出: Dog barks

        myCat.MakeSound();     // 输出: Cat meows

    }

}

在这个例子中,Animal类有一个虚方法MakeSound,子类Dog和Cat重写了这个方法,根据对象的实际类型,调用相应的MakeSound方法,这个就是运行时多态,在编译时调用的是基类方法,而在运行时,根据对象不同,调用不同的子类方法

2,接口实现

接口通过interface声明,接口中只声明不实现

另外,接口成员的访问修饰符默认是public,不能使用private和protected等修饰 符

一个类可以同时继承多个接口

例:

using System;



public interface IAnimal
{

    void MakeSound();

}



public class Dog : IAnimal
{

    public void MakeSound()

    {

        Console.WriteLine("Dog barks");

    }

}



public class Cat : IAnimal
{

    public void MakeSound()
    {

        Console.WriteLine("Cat meows");

    }

}



class Program
{

    static void Main()
    {

        IAnimal myDog = new Dog();

        IAnimal myCat = new Cat();



        myDog.MakeSound();  // 输出: Dog barks

        myCat.MakeSound();  // 输出: Cat meows

    }

}

在这个例子中,IAnimal 接口定义了一个 MakeSound 方法,不同的类(Dog 和 Cat) 实现了该接口。通过接口类型的变量(IAnimal),可以根据对象的实际类型调用不 同的实现

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐