C#语言还是比较常见的东西,这里我们主要介绍C# override和overload区别,包括介绍C# override和overload特点等方面。
C# override和overload特点
重载(Overload)类中定义的方法可能有不同的版本特点:
◆方法名必须相同
◆参数列表必须不相同
◆返回值类型可以不相同
覆写(overwrite)子类为满足自己的需要来重复定义某个方法的不同实现,通过使用关键字override来覆写。特点:
◆相同的方法名称
◆相同的参数列表
◆相同的返回值类型
C# override和overload区别
◆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();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
【编辑推荐】