namespace BaseTest
{
class father
{
string str1 = "this field[1] of baseclass", str2 = "this field[2] of baseclass";
public void F1() //Non-virtual method
{
Console.WriteLine(" F1 of the baseclass");
}
public virtual void F2()//virtual method
{
Console.WriteLine(" F2 of the baseclass");
}
public virtual void F3()
{
Console.WriteLine(" F3 of the baseclass that is not overrided ");
}
public string this[int index]
{
set
{
if (index==1 )
{
str1 = value;
}
else
{
str2 = value;
}
}
get
{
if (index ==1)
{
return str1;
}
else
{
return str2;
}
}
}
}
class Child:father
{
public void G()
{
Console.WriteLine("======Non-virtual methods Test =========");
base.F1();
((father)this).F1();
Console.WriteLine("======virtual methods Test=========");
base.F2();
((father)this).F2();
base.F3();
((father)this).F3();
Console.WriteLine("=====Test the type that the tbase [[expression]] ==========");
Console.WriteLine(base[1]);
base[1] = "override the default ";
Console.WriteLine(base[1]);
Console.WriteLine("================Test Over=====================");
}
public override void F2()
{
Console.WriteLine(" F2 of the subclass ");
}
static void Main(string[] args)
{
Child child=new Child();
child.G();
Console.ReadKey();
}
}
}
base用于构造函数声明,用法和this用于构造函数声明完全一致,但base是对基类构造函数形参的匹配。
using System;
namespace BaseCoTest
{
class Base
{
public Base(int a, string str)
{
Console.WriteLine("Base. Base(int a,string str)");
}
public Base(int a)
{
Console.WriteLine("Base. Base(int a)");
}
public Base()
{
}
}
class Sub : Base
{
public Sub()
{
}
public Sub(int a)
: base(1, "123")
{
Console.WriteLine("Sub .Sub(int a)");
}
class Test
{
public static void Main()
{
Sub sub = new Sub(1);
Console.ReadKey();
}
}
}
}作者: 黑马龙超 时间: 2012-6-18 19:49 本帖最后由 黑马龙超 于 2012-6-18 19:54 编辑