标题: 谁帮我写个接口的例子啊 [打印本页] 作者: 派大星 时间: 2012-2-14 16:18 标题: 谁帮我写个接口的例子啊 听说过接口. 不知道是怎么回事. 和继承的有什么关系呢? 作者: 周平武 时间: 2012-2-14 16:34
接口本身不提供类或结构能够以继承基类功能的方式继承的任何功能。但是,如果基类实现接口,派生类将继承该实现。
测试代码:
interface I
{
void Fun();
}
class A : I
{
public void Fun()
{
}
}
class B:A
{
}
以下测试返回值为true:
B b = new B();
bool bTest = b is I;作者: 许庭洲 时间: 2012-4-28 15:42
//打印接口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");
}