static void Main(string[] args) { Octagon octagon = new Octagon(); IDrawToForm form = octagon; form.Draw(); IDrawToMemory memory = octagon; memory.Draw(); IDrawToPrinter printer = octagon; printer.Draw(); // Сокращенная нотация, если переменная интерфейса не нужна ((IDrawToPrinter)octagon).Draw(); // Можно было бы также использовать ключевое слово as if (octagon is IDrawToMemory) { ((IDrawToMemory)octagon).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); Console.WriteLine(oct); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the "as" keyword. if (oct is IDrawToMemory dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****** Fun with Interface Name Clashes ******\n"); // All of these invocations call // the same Draw() method. Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); // We now must use casting to acces the Draw() members. Console.WriteLine(); ((IDrawToPrinter)oct).Draw(); // Could also use the is keyword if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method. Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToForm)oct).Draw(); // Could also use the "is" keyword. if (oct is IDrawToForm dtm) { dtm.Draw(); } IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon octagon = new Octagon("Octagon"); Circle circle = new Circle("Circle"); // We now must use casting to access the Draw() // members. IDrawToForm drawToForm = (IDrawToForm)octagon; drawToForm.Draw(); drawToForm = (IDrawToForm)circle; drawToForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)octagon).Draw(); // Will draw twice just to show use of as keyword if (octagon is IDrawToMemory) { ((IDrawToMemory)octagon).Draw(); (octagon as IDrawToMemory).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // This is now a compiler error! // oct.Draw(); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface varaible for later use. ((IDrawToPrinter)oct).Draw(); // Always a good practice to test // before extracting an interface. if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface name clashes *****\n"); Octagon oct = new Octagon(); ////all of these use the same Draw() method! //IDrawToForm itfForm = (IDrawToForm)oct; //itfForm.Draw(); //IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; //itfPrinter.Draw(); //IDrawToMemory itfMemory = (IDrawToMemory)oct; //itfMemory.Draw(); //using casting to access Draw()... IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); //shorthand that would work if you don't need the "itfForm" variable above ((IDrawToPrinter)oct).Draw(); //using the "is" keyword if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("*** Fun with intarface name calshes ***\n"); Octagon oct = new Octagon(); IDrowToForm idtf = (IDrowToForm)oct; idtf.Draw(); IDrawToMemory idtm = (IDrawToMemory)oct; idtm.Draw(); IDrawToPrinter idtp = (IDrawToPrinter)oct; idtp.Draw(); ((IDrowToForm)oct).Draw(); if (oct is IDrawToPrinter dp) { dp.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Octagon oct = new Octagon(); //Для доступа к членам Draw() должно использоваться приведение. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); //Сокращенная форма, если переменная интерфейса не нужна. ((IDrawToMemory)oct).Draw(); }
static void WhichInterface() { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("=> which interface"); Octagon oct = new Octagon(); ((IDrawToForm)oct).Draw(); ((IDrawToMemory)oct).Draw(); ((IDrawToPrinter)oct).Draw(); }
static void Main(string[] args) { Octagon oct = new Octagon(); IDrawToForm itForm = (IDrawToForm)oct; IDrawToMemory itMemory = (IDrawToMemory)oct; IDrawToPrinter itPrinter = (IDrawToPrinter)oct; //oct.Draw(); ERROR, interfaces are implemented explicitely itForm.Draw(); itMemory.Draw(); itPrinter.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Octagon oct = new Octagon(); //oct.Draw(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method! Octagon oct = new Octagon(); //oct.Draw() is no longer available w/o cast IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToPrinter itfPriner = (IDrawToPrinter)oct; itfPriner.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun With Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // We now myst use casting to access the Draw() members IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need the inerface // variable for later use ((IDrawToPrinter)oct).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToMemory)oct).Draw(); if (oct is IDrawToPrinter) { ((IDrawToPrinter)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Claches *****\n"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToPrinter)oct).Draw(); if (oct is IDrawToMemory dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Fun with interface name clashes ****"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); ((IDrawToMemory)oct).Draw(); if (oct is IDrawToPrinter) { ((IDrawToPrinter)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Octagon oct = new Octagon(); IDrawToForm octF = (IDrawToForm)oct; octF.Draw(); IDrawToMemory octM = (IDrawToMemory)octF; octM.Draw(); IDrawToPrinter octP = (IDrawToPrinter)octF; octM.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Octagon octagon = new Octagon(); IDrawForm itfForm = (IDrawForm)octagon; itfForm.Draw(); IDrawMemory itfMemory = (IDrawMemory)octagon; itfMemory.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)octagon; itfPrinter.Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm) oct; itfForm.Draw(); ((IDrawToPrinter) oct).Draw(); if (oct is IDrawToMemory) { ((IDrawToMemory) oct).Draw(); } Console.ReadLine(); }
static void Main( string[] args ) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // We now must use casting to access the Draw() // members. IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToPrinter)oct).Draw(); // Could also use the "as" keyword. if (oct is IDrawToMemory) ((IDrawToMemory)oct).Draw(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Clashes *****"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); //Сокращенная форма ((IDrawToMemory)oct).Draw(); // Можно с is if (oct is IDrawToPrinter dtm) { dtm.Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); // 现在必须使用转换来访问Draw()成员 IDrawToForm itform = (IDrawToForm)oct; itform.Draw(); // 如果以后不需要接口变量,可以简化成这个形式 ((IDrawToPrinter)oct).Draw(); // 也可以用"is"关键字 if (oct is IDrawToMemory) { ((IDrawToMemory)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); // All of these invocations call the // same Draw() method! Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); IDrawToPrinter itfPrinter = (IDrawToPrinter)oct; itfPrinter.Draw(); // Must use casting to access the Draw() when explicitly implemented. IDrawToForm itfForm2 = (IDrawToForm)oct; itfForm2.Draw(); // Shorthand notation if you don't need // the interface variable for later use. ((IDrawToMemory)oct).Draw(); // Could also use the "is" keyword. if (oct is IDrawToPrinter) { ((IDrawToPrinter)oct).Draw(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Fun with Interface Name Clashes *****\n"); Octagon oct = new Octagon(); IDrawToForm itfForm = (IDrawToForm)oct; itfForm.Draw(); /* * ((IDrawToPrinter)oct).Draw(); * * if (oct is IDrawToMemory) * ((IDrawToMemory)oct).Draw(); */ IDrawToPrinter itfPriner = (IDrawToPrinter)oct; itfPriner.Draw(); IDrawToMemory itfMemory = (IDrawToMemory)oct; itfMemory.Draw(); Console.ReadLine(); }