/// 测试点
/// (1) Override 与 new 的区别 (new后方法在父类与子类中各自存在,Override覆盖父类中函数)
/// (1) Override 与 new 的区别 (new后方法在父类与子类中各自存在,Override覆盖父类中函数)
/// (2) 子类 构造函数 的执行时间 (先执行父类构造函数,再执行子类构造函数)
public class baseClass
{
public baseClass()
{
MethodOfOverride();
MethodOfNew();
}
public virtual void MethodOfOverride()
{
Console.WriteLine("baseClass:MethodOfOverride");
}
public void MethodOfNew()
{
Console.WriteLine("baseClass:MethodOfNew");
}
}
public class derivedClass : baseClass
{
public int value;
public derivedClass()
{
value = 2;
}
public override void MethodOfOverride()
{
Console.WriteLine("derivedClass:MethodOfOverride value=" + value.ToString());
}
public new void MethodOfNew()
{
Console.WriteLine("derivedClass:MethodOfNew value=" + value.ToString());
}
}
/// <summary>
/// 测试点
/// (1) Override 与 new 的区别 (new后方法在父类与子类中各自存在,Override覆盖父类中函数)
/// (2) 子类 构造函数 的执行时间 (先执行父类构造函数,再执行子类构造函数)
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********baseClass tempClassA = new baseClass()*********");
Console.WriteLine("----------------初始化输出----------------------");
baseClass tempClassA = new baseClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassA.MethodOfOverride();
tempClassA.MethodOfNew();
Console.WriteLine("*********derivedClass tempClassB = new derivedClass()*********");
Console.WriteLine("----------------下面初始化输出----------------------");
derivedClass tempClassB = new derivedClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassB.MethodOfOverride();
tempClassB.MethodOfNew();
Console.WriteLine("*********baseClass tempClassC = new derivedClass()*********");
Console.WriteLine("----------------下面初始化输出----------------------");
baseClass tempClassC = new derivedClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassC.MethodOfOverride();
tempClassC.MethodOfNew();
Console.Read();
//
/* 输出结果
*********baseClass tempClassA = new baseClass()*********
----------------初始化输出----------------------
baseClass:MethodOfOverride
baseClass:MethodOfNew
----------------下面方法输出------------------------
baseClass:MethodOfOverride
baseClass:MethodOfNew
*********derivedClass tempClassB = new derivedClass()*********
----------------初始化输出----------------------
derivedClass:MethodOfOverride value=0
baseClass:MethodOfNew
----------------下面方法输出------------------------
derivedClass:MethodOfOverride value=2
derivedClass:MethodOfNew value=2
*********baseClass tempClassC = new derivedClass()*********
----------------初始化输出----------------------
derivedClass:MethodOfOverride value=0
baseClass:MethodOfNew
----------------下面方法输出------------------------
derivedClass:MethodOfOverride value=2
baseClass:MethodOfNew
*/
}
}
{
public baseClass()
{
MethodOfOverride();
MethodOfNew();
}
public virtual void MethodOfOverride()
{
Console.WriteLine("baseClass:MethodOfOverride");
}
public void MethodOfNew()
{
Console.WriteLine("baseClass:MethodOfNew");
}
}
public class derivedClass : baseClass
{
public int value;
public derivedClass()
{
value = 2;
}
public override void MethodOfOverride()
{
Console.WriteLine("derivedClass:MethodOfOverride value=" + value.ToString());
}
public new void MethodOfNew()
{
Console.WriteLine("derivedClass:MethodOfNew value=" + value.ToString());
}
}
/// <summary>
/// 测试点
/// (1) Override 与 new 的区别 (new后方法在父类与子类中各自存在,Override覆盖父类中函数)
/// (2) 子类 构造函数 的执行时间 (先执行父类构造函数,再执行子类构造函数)
/// </summary>
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********baseClass tempClassA = new baseClass()*********");
Console.WriteLine("----------------初始化输出----------------------");
baseClass tempClassA = new baseClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassA.MethodOfOverride();
tempClassA.MethodOfNew();
Console.WriteLine("*********derivedClass tempClassB = new derivedClass()*********");
Console.WriteLine("----------------下面初始化输出----------------------");
derivedClass tempClassB = new derivedClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassB.MethodOfOverride();
tempClassB.MethodOfNew();
Console.WriteLine("*********baseClass tempClassC = new derivedClass()*********");
Console.WriteLine("----------------下面初始化输出----------------------");
baseClass tempClassC = new derivedClass();
Console.WriteLine("----------------下面方法输出------------------------");
tempClassC.MethodOfOverride();
tempClassC.MethodOfNew();
Console.Read();
//
/* 输出结果
*********baseClass tempClassA = new baseClass()*********
----------------初始化输出----------------------
baseClass:MethodOfOverride
baseClass:MethodOfNew
----------------下面方法输出------------------------
baseClass:MethodOfOverride
baseClass:MethodOfNew
*********derivedClass tempClassB = new derivedClass()*********
----------------初始化输出----------------------
derivedClass:MethodOfOverride value=0
baseClass:MethodOfNew
----------------下面方法输出------------------------
derivedClass:MethodOfOverride value=2
derivedClass:MethodOfNew value=2
*********baseClass tempClassC = new derivedClass()*********
----------------初始化输出----------------------
derivedClass:MethodOfOverride value=0
baseClass:MethodOfNew
----------------下面方法输出------------------------
derivedClass:MethodOfOverride value=2
baseClass:MethodOfNew
*/
}
}