publicclass Test1
{
private int _age=10;
public int Age
{
get { return _age; }
set { _age = value; }
}
public virtual void Output1(string name)
{
name ="Test1";
Console.Write("Test1.Output1 :Name is "+ name);
}
public virtual void Output2(ref stringrefname)
{
refname = "Test1";
Console.Write("Test1.Output2 :Name is " + refname);
}
}
public class Test2:Test1
{
private int _age=20;
public override void Output1(stringname)
{
name ="Test2";
Console.Write("Test2.Output1 :Name is "+ name);
}
public new void Output2(ref stringrefname)
{
refname = "Test2";
Console.Write("Test2.Output2 :Name is " + refname);
}
}
string name = "Name";
string refname = "RefName";
Test1 t1 = new Test2();
t1.Output1(name); //输出1?
t1.Output2(ref refname); // 输出2?
Console.Write(name); // 输出3?
Console.Write(refname); // 输出4?
Test2 t2 = new Test2();
Console.Write(t2.Age.ToString()); //输出5以及是否装箱?
t2.Output2(ref refname); //输出6
Test2类重写了Test1类的OutPut1方法,隐藏了OutPut2方法,因此t1.OutPut1(name)输出应该调用的是重写后的方法即t2中的方法,输出应为:est2.Output1 :Name is Test2;t1.OutPut2(ref refname)输出应该是:Test1.Output2 :Name is Test1。Console.Write(name)输出为:Name;Console.Write(refName)应为Test2。Console.Write(t2.Age.ToString())不是很清楚。t2.Output2(ref refname)输出:Test2.Output2 :Name is test2