Mohammad_Rahimirad
سه شنبه 04 تیر 1387, 10:36 صبح
خط ** تو Main داره Cast می کنه،خوب دقیقا داره چه می کنه؟؟؟
// Draw image to a Form.
public interface IDrawToForm
{
void Draw();
}
// Draw to buffer in memory.
public interface IDrawToMemory
{
void Draw();
}
// Render to the printer.
public interface IDrawToPrinter
{
void Draw();
}
------------------
class Octagon : IDrawToForm, IDrawToMemory, IDrawToPrinter
{
// Explicitly bind Draw() implementations
// to a given interface.
void IDrawToForm.Draw()
{
Console.WriteLine("Drawing to form...");
}
void IDrawToMemory.Draw()
{
Console.WriteLine("Drawing to memory...");
}
void IDrawToPrinter.Draw()
{
Console.WriteLine("Drawing to a printer...");
}
}
-----------------
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();
}
// Draw image to a Form.
public interface IDrawToForm
{
void Draw();
}
// Draw to buffer in memory.
public interface IDrawToMemory
{
void Draw();
}
// Render to the printer.
public interface IDrawToPrinter
{
void Draw();
}
------------------
class Octagon : IDrawToForm, IDrawToMemory, IDrawToPrinter
{
// Explicitly bind Draw() implementations
// to a given interface.
void IDrawToForm.Draw()
{
Console.WriteLine("Drawing to form...");
}
void IDrawToMemory.Draw()
{
Console.WriteLine("Drawing to memory...");
}
void IDrawToPrinter.Draw()
{
Console.WriteLine("Drawing to a printer...");
}
}
-----------------
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();
}