好继承…
假设您有以下课程:
class A { public int Foo(){ return 5;} public virtual int Bar(){return 5;}}class B : A{ public new int Foo() { return 1;} //shadow public override int Bar() {return 1;} //override}然后,当您致电:
A clA = new A();B clB = new B();Console.WriteLine(clA.Foo()); // output 5Console.WriteLine(clA.Bar()); // output 5Console.WriteLine(clB.Foo()); // output 1Console.WriteLine(clB.Bar()); // output 1//now let's cast B to an A classConsole.WriteLine(((A)clB).Foo()); // output 5 <<<-- shadowConsole.WriteLine(((A)clB).Bar()); // output 1
假设您有一个基类,并且在所有代码中都使用了基类而不是继承的类,并且使用了影子,它将返回基类返回的值,而不是遵循对象实际类型的继承树。
在这里运行代码
希望我有道理:)



