演示代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
private static void Main(string[] args)
{
ITestzh itestzh = new Testzh();
ITestzh itestdemo = new TestDemo();
itestzh.Insert(); //输出“子类重写父类的Insert方法”
itestdemo.Insert(); //输出”用new隐藏父类的Inser方法“
TestBase t1 = new Testzh();
TestBase t2 = new TestDemo();
t1.Insert(); //输出“子类重写父类的Insert方法”
t2.Insert(); //输出”基类统一实现Insert方法“
//注意 t2.Insert()与itestdemo.Insert()输出的区别
Console.ReadKey();
}
}
//借口统一规范
interface ITest
{
/// <summary>
/// 插入方法
/// </summary>
void Insert();
}
/// <summary>
/// 统一的实体基类
/// 子类共同的操作逻辑
/// </summary>
abstract class TestBase : ITest
{
#region ITest 成员
public virtual void Insert()
{
Console.WriteLine("基类统一实现Insert方法");
}
#endregion
}
//声明借口实现ITest借口
interface ITestzh:ITest{}
/// <summary>
/// Testzh实体操作
/// </summary>
class Testzh:TestBase,ITestzh
{
//重写父类的Insert方法
public override void Insert()
{
Console.WriteLine("子类重写父类的Insert方法");
}
}
class TestDemo : TestBase, ITestzh
{
public new void Insert()
{
Console.WriteLine("用new隐藏父类的Inser方法");
}
}
}