An interesting piece of code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1

{
class A
{
public virtual void Movie()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void Movie()
{
Console.WriteLine("B");
}
}
class C : B
{
public new virtual void Movie()
{
Console.WriteLine("C");

}

}
class D : C
{
public override void Movie()
{
Console.WriteLine("D");
}

}
class Program
{
static void Main(string[] args)
{
A objA = new D();
A objB = new B();
C objC = new D();
A objD = new A();
objA.Movie();
objB.Movie();
objC.Movie();
objD.Movie();
Console.ReadLine();
}
}
}

the reason is like:

B overrides A ,it‘s not virtual.

so the inherited one stops when it can‘t go further or it reach to itself.

时间: 2024-10-03 22:25:16

An interesting piece of code的相关文章

Exploring Python Code Objects

Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Inspired by David Beazley's Keynote at PyCon, I've been digging around in code objects in Python lately. I don't have a particular axe to grind, nor some

Code is not literature

http://www.gigamonkeys.com/code-reading/ I have started code reading groups at the last two companies I’ve worked at, Etsy and Twitter, and some folks have asked for my advice about code reading and running code reading groups. Tl;dr: don’t start a c

Async方法死锁的问题 Don't Block on Async Code(转)

今天调试requet.GetRequestStreamAsync异步方法出现不返回的问题,可能是死锁了.看到老外一篇文章解释了异步方法死锁的问题,懒的翻译,直接搬过来了. http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html This is a problem that is brought up repeatedly on the forums and Stack Overflow. I think it’s t

Don't Block on Async Code

http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html This is a problem that is brought up repeatedly on the forums and Stack Overflow. I think it’s the most-asked question by async newcomers once they’ve learned the basics. UI Example

poj 3959 Alignment of Code

Description You are working in a team that writes Incredibly Customizable Programming Codewriter (ICPC) which is basically a text editor with bells and whistles. You are working on a module that takes a piece of code containing some definitions or ot

Calling C++ code from C# z

http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for this section (although I suppose my average posting frequency is already pretty low), but I was on a much needed vacation. I finished the last chapter wi

Building Maintainable Software-java篇之Write Short Units of Code

Building Maintainable Software-java篇之Write Short Units of Code Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -Martin Fowler Guideline: ? Limit the length of code units to 15 lines of c

Building Maintainable Software-java篇之 Write Clean Code

Building Maintainable Software-java篇之 Write Clean Code Writing clean code is what you must do in order to call yourself a professional. -Robert C. Martin Guideline: ? Write clean code. ? Do this by not leaving code smells behind after development wor

Read Code

Read Code Karianne Berg WE PROGRAMMERS ARE WEiRD CREATURES. We love writing code. But when it comes to reading it, we usually shy away. After all, writing code is so much more fun, and reading code is hard-sometimes almost impossible. Reading other p