方法的重写:是指在继承关系中不同类之间方法之间的比较和研究
第一个关键字:virtual(虚)
用virtual修饰的方法称之为虚方法
virtual一般情况下需要和override成对出现
如果子类重写使用了override,则父类必须拥有virtual修饰的方法
反过来不一定成立,因为我们还有方法覆盖
第二个关键字:override(重写)
为什么要使用方法重写呢?
1.使用虚方法其实就是对目前的程序不是很满意,暂时方法的功能是可以实现的,希望后面的程序员
通过重写父类方法的方式,进行功能上的优化。
2.可以通过继承系统提供的类,去修改系统类中提供的虚方法
方法重写需要注意的地方:
1.不能重写父类中的sealed修饰的方法
2.不能重写父类中用static修饰的方法
3.不能重写父类中存在abstract方法,子类必须重写
using System;
using System.Diagnostics;
namespace 方法重写
{
class BaseClass
{
public virtual void SayHi()
{
Console.WriteLine ("BaseClass 虚方法调用");
}
//
// public static virtual void AAA()
// {
//
// }
}
class SubClass:BaseClass
{
public override void SayHi ()
{
Console.WriteLine ("SubClass 重写父类 SayHi");
}
}
//
// class Catamount{
// public virtual void Shout(){
// Console.WriteLine ("猫科动物叫!~");
// }
// }
//1.
// class Cat:Catamount{
// public override void Shout(){
// Console.WriteLine ("猫叫!~");
// }
// }
// class Tiger:Catamount{
// public override void Shout(){
// Console.WriteLine ("老虎叫!~");
// }
// }
// class Lion:Catamount{
// public override void Shout(){
// Console.WriteLine ("狮子叫!~");
// }
// }
//2.
class Convert{
public virtual float MConvert(float km){
return km * 0.62f;
}
}
class Convert1:Convert{
public float KConvert(float kg){
return kg * 2.2f;
}
}
// 3.
class Convert2:Convert{
public override float MConvert (float km)
{
return base.MConvert (km);
}
}
// 4.
class Area:Convert{
public override float MConvert (float km)
{
float x = base.MConvert (km);
float mj = x * x;
return mj;
}
}
// 5.
class To{
public override string ToString ()
{
return string.Format ("[To]");
}
}
// 6.
class Paixu
{
public virtual void Pai(int[] array){
int temp = 0;
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length; j++)
{
if (array [i] > array [j])
{
array [i] = temp;
array [i] = array [j];
array [j] = temp;
}
}
}
}
}
class Tongpai:Paixu
{
public override void Pai (int[] array)
{
Array.Sort (array);
}
}
// class Tool{
// public int[]
// }
class MainClass
{
public static void Main (string[] args)
{
// //创建父类对象
new BaseClass().SayHi();
// //创建子类对象
new SubClass().SayHi();
//练习1.猫科动物(猫,老虎,狮子),具有叫的行为,子类也有叫的方法,但是每个子类的行为却不一样,用多态解决该问题
// new Catamount().Shout();
// new Cat ().Shout ();
// new Tiger ().Shout ();
// new Lion ().Shout ();
//练习2.设计一个基类MConvert,这个方法接受一个代表公里的参数,将其转换成对等的英里数,然后创建子类,增加可以将
//公斤转换成磅的新方法KConvert,测试子类调用其自身以及父类的功能。1公里 = 0.62英里 1公斤 =2.2磅
Convert c = new Convert();
Convert1 c1 = new Convert1 ();
Console.WriteLine (c.MConvert (5) + "英里");
Console.WriteLine (c1.KConvert (1) + "磅");
// 练习3.调整上一题的内容,将其中MConvert方法声明为虚方法,在子类中进行重写
Convert2 c2 = new Convert2();
Console.WriteLine (c2.MConvert(1) + "英里");
//练习4.接收参数为正方形边长,转换为英里后计算其面积
Area area = new Area();
Console.WriteLine (area.MConvert(1) + "平方英里");
//练习5.创建一个类重写ToString()方法
Console.WriteLine (new To().ToString());
//练习6.设计一个工具类可以排序数组,并且设计一个可以统计方法运行耗费总时的一个属性或者方法,优化这个工具类排序的方法
// 并且调用显示耗费时间的变量来做前后的时间差对比
Stopwatch sw = new Stopwatch ();
int[] array = { 1, 5, 89, 6, 4, 3, 45, 94 };
sw.Start ();
new Paixu ().Pai (array);
sw.Stop ();
Console.WriteLine (sw.Elapsed);
Stopwatch sw2 = new Stopwatch ();
sw2.Start ();
new Tongpai ().Pai (array);
sw2.Stop ();
Console.WriteLine (sw2.Elapsed);
}
}
}