(1)使用Virtual关键字Override从写
父类子类代码如下,不能修改public 为其它权限
public virtual void Clear() { UpdateView(); }
public override void Clear() { base.Clear(); }
(2)使用new覆盖
可以从写public 为private
public void Clear() { UpdateView(); } private new void Clear() { base.Clear(); }
示例:
namespace ConsoleApplication2 { class Program { static void Main(string[] args) { ExtUtil v=new ExtUtil(); v.Clear(); Console.ReadLine(); } } public class BaseUtil { public void Clear() { Console.WriteLine("a"); } } public class ExtUtil:BaseUtil { public new void Clear() { Console.WriteLine("b"); } } }
输出b