override和overload的区别
2023-07-06
override 表示重写,用于继承类对基类中虚成员的实现
overload 表示重载,用于同一个类中同名方法不同参数(包括类型不同或个数不同)的实现
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example07
class Program
class BaseClass
public virtual void F()
Console.WriteLine(“BaseClass.F”);
class DeriveClass : BaseClass
public override void F()
base.F();
Console.WriteLine(“DeriveClass.F”);
public void Add(int Left, int Right)
Console.WriteLine(“Add for Int: 0”, Left + Right);
public void Add(double Left, double Right)
Console.WriteLine(“Add for int: 0”, Left + Right);
static void Main(string[] args)
DeriveClass tmpObj = new DeriveClass();
tmpObj.F();
tmpObj.Add(1, 2);
tmpObj.Add(1.1, 2.2);
Console.ReadLine();
结果:
BaseClass.F
DeriveClass.F
Add for Int: 3
Add for int: 3.3