KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
when use new virtual key words to decorate the method? what is the affection? Like define an interface, and add a class to inherit the interface. but use the new virtual to realize the interface method. interface IPrinter { void Print(); } public class PrinterOne : IPrinter { public void Print() { Console.WriteLine("PrinterOne."); } } public class PrinterTwo : PrinterOne { public new virtual void Print() { Console.WriteLine("PrinterTwo."); } } public class PrinterThree : PrinterTwo { public override void Print() { Console.WriteLine("PrinterThree."); } } public class PrinterFour : PrinterThree { public override void Print() { Console.WriteLine("PrinterFour."); } } static void Main(string[] args) { IPrinter iprinter = new PrinterFour(); iprinter.Print();//the output is PrinterOne? why??? Console.ReadLine(); }
Tags (comma-separated)
Save Edits
Cancel