在C#中,什么是多态?如何实现?
C#中的多态性
多态性是面向对象编程的一个核心概念,他允许对象以多种形式表现.在C#中,多态主要通过虚方法,抽象方法和接口来实现. 多态性的存在使得同一个行为可以有多个不同的表达形式 即同一个接口可以使用不同的实例来执行不同的操作
虚方法(Virtual Methods)
在C#中,可以通过在基类中定义虚方法,然后再派生类中重写这些方法来实现多态. 虚方法使用Virtual 关键字声明,然后在派生类中通过使用 override关键字重写
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace 虚方法
{internal class Program{static void Main(string[] args){}}class Animals{public string Name { get; set; }public virtual void Speak(){Console.WriteLine("说话");}}class Dog:Animals{public override void Speak(){Console.WriteLine("汪汪汪");}}class Cat : Animals{public override void Speak(){Console.WriteLine("喵喵喵");}}
}
// 总结:抽象方法和虚方法有什么区别?
//1.抽象方法只能定义在抽象类中,虚方法可以定义在普通的类中
//2.抽象方法不可以有内容,必须在派生类中实现,虚方法可以有内容,可以不再派生类中实现,不实现则使用基类的方法
抽象类 (Abstract Classes)
抽象类是不能被实例化的类,它通常包含一个或者多个抽象方法. 抽象方法只有声明没有实现的方法,他们必须在派生类中被重写. 抽象类和方法使用 abstract关键字声明
abstract class Pay{public int Id { get; set; }public string Count { get; set; }private string main;public abstract void payment(double money);public abstract void payyment(double money, string main);public string Main{get => Main;set{if (value != "微信" || value != "支付宝"){throw new Exception("请选择支付方式");}else{value = Main;}}}}class WeCht : Pay{public override void payment(double money){Console.WriteLine($"使用微信支付,你的账户{Id},密码{Count},你需要付{money}元");}public override void payyment(double money, string main){Console.WriteLine($"使用{main}支付,你的账户{Id},密码{Count},你需要付{money}元");}}class APay : Pay{public override void payment(double money){Console.WriteLine($"使用支付宝支付,你的账户{Id},密码{Count},你需要付{money}元");}public override void payyment(double money, string main){Console.WriteLine($"使用{main}支付,你的账户{Id},密码{Count},你需要付{money}元");}}
// 总结:抽象类和普通类的区别?
//1.抽象类不能实例化,普通类可以实例化
//2.抽象类中不一定要有抽象方法,也可以没有抽象方法,普通的类不能由抽象方法
//3.抽象类可以和普通类一样,有普通方法
//4.抽象类可以继承抽象类,子类如果不是抽象类,必须重写抽象类的全部抽象方法
//5.抽象方法就是被子类重写的,所以不能使用private修饰符
接口:C#接口(Interface)-CSDN博客
析构函数~
析构函数的定义:一个和类相同的方法 , 在类名的前面加上~
析构函数的主要作用是释放对象的资源,但是析构函数是由垃圾回收控制的,无法做到显示的调用,所以使用析构函数释放资源不是良好访问
internal class Program{static void Main(string[] args){People people = new People();people=null;//强制启动垃圾回收机制GC.Collect();Console.ReadLine();}}class People{~People(){Console.WriteLine("析构函数执行");}}
重载
static void Main(string[] args){//多态:指同一个具有不同的表现形式和能力//多态性又可以是静态的或者动态的,静态指的是编译过程中发生的,动态指的是运行过程中发生的//静态多态:在编译过程中,通过方法重载和运算符重载实现多态,也叫做静态绑定//动态多态:通过抽象方法,重写方法,隐藏方法实现运行时多态,叫做动态绑定new People();People people = new People();new People(12);new People("Kitty");//总结: 方法重载//1.通过一个方法不同的重载可以是参数的数量不同//2.也可以是参数的类型不同//3.和返回值类型无关(不同返回值类型,不算重载)}}class People{public People() { }public People(int id) { }public People(string name) { }public void Eat() { }public void Eat(int id) { }}class Number{public double Length { get; set; }public double Width { get; set; }public double value { get { return Width*Length; } }//重载+运算符//格式: public static 返回值类型 operator 要重载的运算符(参数列表)//public static double operator+(Number a1, Number a2)//{// return a1.value + a2.value;//}public static Number operator +(Number a1, Number a2){return new Number{Width = a1.Width + a2.Width,Length = a1.Length + a2.Length};}public static Number operator -(Number a1, Number a2){return new Number{Width = a1.Width - a2.Width,Length = a1.Length - a2.Length};}public static bool operator >(Number a1, Number a2){return a1.value > a2.value;}public static bool operator <(Number a1, Number a2){return a1.value < a2.value;}}
}
在C#中,多态性还可以通过其他方式实现,如方法重载和运算符重载,这些都是静态多态性的例子,它们在编译时就已经确定了。而虚方法、抽象类和接口提供的是动态多态性,它们允许在运行时确定具体调用哪个方法。
多态性的好处在于它提高了程序的可扩展性和可维护性,使得代码更加灵活和可重用。例如,可以编写一个函数,它接受一个 Shape 类型的参数,并调用其 Draw 方法,不管传入的是 Circle 还是 Rectangle 对象,都能正确调用相应的 Draw 方法,这就是多态性的魅力。