//打印接口IPrint
public interface IPrint
{
void Print();
void PrintPreview();
}
//调用接口的主程序
static void Main()
{
Console.WriteLine("Please select printer: ");
string printerName=Console.ReadLine();
IPrint printer=null;
if(printerNmae=="HP")
{
printer=new HPPrinter();
}
else if(printerName=="IBM")
{
printer=new IBMPrinter();
}
else if(printerName=="Epsom")
{
printer=new EpsomPrinter();
}
printer.PrintPreview();
printer.Print();
}
//HPPrint类
public class HPPrint:IPrint
{
public void PrintPreview()
{
Console.WriteLine("this is HP Printer");
}
public void Print()
{
Console.WriteLine("this is HP Printer");
}
}
//IBMPrint类
public class IBMPrint:IPrint
{
public void PrintPreview()
{
Console.WriteLine("this is IBM Printer");
}
public void Print()
{
Console.WriteLine("this is IBM Printer");
}
}
//EpsomPrint类
public class EpsomPrint:IPrint
{
public void PrintPreview()
{
Console.WriteLine("this is Epsom Printer");
}
public void Print()
{
Console.WriteLine("this is Epson Printer");
}
}
这就是接口的作用,希望对你有帮助。
|