110,534
社区成员
发帖
与我相关
我的任务
分享
class Father
{
public Father()
{
Console.WriteLine("Father...");
}
private string GetName()
{
return "Father Name";
}
public string Move()
{
return "West";
}
public void Call()
{
Console.WriteLine(String.Format("This is {0}, and go {1}", GetName(), Move()));
}
public virtual void MyFunc(string str)
{
Console.WriteLine("{0} in Base", str);
}
}
class Child : Father
{
public Child()
{
Console.WriteLine("Children...");
}
private string GetName()
{
return "Children Names";
}
public string Move()
{
return "Far East";
}
public new void MyFunc(string str)
{
base.MyFunc(str);
Console.WriteLine("{0} in Derived", str);
}
public int ChildAge = 0;
}
static void Main(string[] args)
{
Father B = new Father();
B.MyFunc("BBB");
Console.WriteLine("-------------------------");
Child A = new Child();
B = A;//B的引用指向A
A.MyFunc("AAA");
Console.WriteLine("-------------------------");
B.MyFunc("BBB1");
Console.WriteLine("-------------------------");
Father C = new Child();
C.MyFunc("CCC");
Console.WriteLine("");
Console.WriteLine("");
Father hiren = new Father();
hiren.Call();
Child heria = new Child();
heria.Call();
Father newHi = heria;
newHi.Call();
}
class Child : Father
{
public Child()
{
Console.WriteLine("Children...");
}
private string GetName()
{
return "Children Names";
}
public string Move() // 这里有问题,这里就应该用new,否则会有无声无息的隐藏父类方法
{
return "Far East";
}
public new void MyFunc(string str)
{
base.MyFunc(str);
Console.WriteLine("{0} in Derived", str);
}
public int ChildAge = 0;
}