using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString2());
a = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString2());
Console.WriteLine(((B)a).ToString2());
Console.ReadLine();
}
}
class A
{
public override string ToString()
{
return "A.tostring()";
}
public string ToString2()
{
return "A.tostring2()";
}
}
class B : A
{
public override string ToString()
{
return "B.tostring()";
}
public new string ToString2()
{
return "B.tostring2()";
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
A a = new A();
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString2());
a = new B();
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString2());
Console.WriteLine(((B)a).ToString2());
Console.ReadLine();
}
}
class A
{
public override string ToString()
{
return "A.tostring()";
}
public string ToString2()
{
return "A.tostring2()";
}
}
class B : A
{
public override string ToString()
{
return "B.tostring()";
}
public new string ToString2()
{
return "B.tostring2()";
}
}
}
结果:
A.tostring()
A.tostring2()
B.tostring()
A.tostring2()
B.tostring2()