public abstract class A { public abstract void Do(); public virtual void DrawLine() { Console.WriteLine("A:DrawLine"); } public virtual void DrawPoint() { Console.WriteLine("A:DrawPoint"); } public virtual void DoWork() { Console.WriteLine("A:DoWork"); } } public class B : A { public override void Do() { Console.WriteLine("B:DO"); } public new void DrawLine() { Console.WriteLine("B:DrawLine"); } public override void DrawPoint() { Console.WriteLine("B:DrawPoint"); } public override void DoWork() { Console.WriteLine("B:DoWork"); } } public class C : B { public sealed override void DoWork() { Console.WriteLine("C:DoWork"); } } public class D : C { // Attempting to override DoWork causes compiler error CS0239. //public override void DoWork() {} public new void DoWork() { Console.WriteLine("D:DoWork"); } } // Only accessible within the same assembly internal class MyInternalClass { public string Value { get; set; } } //public sealed class MySealedClass { } //public class Derived : MySealedClass { } // error CS0509: '': cannot derive from sealed type interface I { void M(); } abstract class AC : I { public abstract void M(); } public class Base { public virtual void DoWork(int param) { Console.WriteLine("Base:DoWork(int)"); } } public class Derived : Base { public override void DoWork(int param) { Console.WriteLine("Derived:DoWork(int)"); } public void DoWork(double param) { Console.WriteLine("Derived:DoWork(double)"); } } A a = new B(); a.DrawLine(); // output A:DrawLine a.DrawPoint(); // output B:DrawPoint a.Do(); // output B:DO D d = new D(); d.DoWork(); // output D:DoWork C c = (C)d; c.DoWork(); // output C:DoWork B b = (B)d; b.DoWork(); // output C:DoWork int val = 5; Derived der = new Derived(); der.DoWork(val); // Calls DoWork(double). ((Base)der).DoWork(val); // Calls DoWork(int) on Derived.
Modifiers: virtual, override, new, abstract, sealed, internal,布布扣,bubuko.com
Modifiers: virtual, override, new, abstract, sealed, internal
原文地址:http://blog.csdn.net/chuwachen/article/details/37692355