Interface定义及使用
2007-04-21 09:50
197 查看
接口定义以大写字母I开头。方法只定义其名称,在C#中,方法默认是公有方法;用public修饰方法是不允许的,否则会出现编译错误;接口可以从别的接口继承,如果是继承多个接口,则父接口列表用逗号间隔。
接口可以通过类来实现,当类的基列表同时包含基类和接口时,列表中首先出现的是基类;类必须要实现其抽象方法;
接口使用:见代码(转)
using System;
namespace Dage.Interface
{
//打印机接口
public interface IPrint
{
string
returnPrintName();
}
}
//--------------------------------------------
using
System;
using Dage.Interface;
namespace
Dage.Print
{
//HP牌打印机类
public class HP: IPrint
{
public
string returnPrintName()
{
return "这是HP牌打印机";
}
}
}
//--------------------------------------------
using
System;
namespace Dage.Print
{
//Eps牌打印机类
public class Eps:
IPrint
{
public string returnPrintName()
{
return
"这是Eps牌打印机";
}
}
}
//--------------------------------------------
using
System;
using Dage.Interface;
namespace Dage
{
//打印类
public
class Printer
{
public Printer()
{}
public string
PrintName(IPrint iPrint)
{
return iPrint.returnPrintName();
}
}
}
//--------------------------------------------
--WinFrom中调用代码:
private
void button1_Click(object sender, System.EventArgs e)
{
Printer p= new
Printer();
switch (this.comboBox1.Text)
{
case "HP":
MessageBox.Show(p.PrintName(new HP()));
break;
case "Eps":
MessageBox.Show(p.PrintName(new Eps()));
break;
default:
MessageBox.Show("没有发现这个品牌!");
break;
}
}
接口可以通过类来实现,当类的基列表同时包含基类和接口时,列表中首先出现的是基类;类必须要实现其抽象方法;
接口使用:见代码(转)
interface使用
interface使用(实例一)using System;
namespace Dage.Interface
{
//打印机接口
public interface IPrint
{
string
returnPrintName();
}
}
//--------------------------------------------
using
System;
using Dage.Interface;
namespace
Dage.Print
{
//HP牌打印机类
public class HP: IPrint
{
public
string returnPrintName()
{
return "这是HP牌打印机";
}
}
}
//--------------------------------------------
using
System;
namespace Dage.Print
{
//Eps牌打印机类
public class Eps:
IPrint
{
public string returnPrintName()
{
return
"这是Eps牌打印机";
}
}
}
//--------------------------------------------
using
System;
using Dage.Interface;
namespace Dage
{
//打印类
public
class Printer
{
public Printer()
{}
public string
PrintName(IPrint iPrint)
{
return iPrint.returnPrintName();
}
}
}
//--------------------------------------------
--WinFrom中调用代码:
private
void button1_Click(object sender, System.EventArgs e)
{
Printer p= new
Printer();
switch (this.comboBox1.Text)
{
case "HP":
MessageBox.Show(p.PrintName(new HP()));
break;
case "Eps":
MessageBox.Show(p.PrintName(new Eps()));
break;
default:
MessageBox.Show("没有发现这个品牌!");
break;
}
}
相关文章推荐
- Interface定义及使用
- 【interface】C#interface定义及使用浅析
- java入门学习(11)—接口定义(interface)及使用(implements)
- Interface定义及使用
- FORTRAN-main程序使用module无需外部定义interface
- java利用@interface定义元注解及使用实例
- Java中接口(Interface)的定义和使用
- C#interface定义及使用浅析
- IE6下使用CSS定义DIV高度行之有效的办法
- JavaScript中定义对象原型的两种使用方法
- 使用typedef语句定义数组类型
- 查看一个定义的方法在哪些地方被使用过(vs2008)
- 使用 Swagger 文档化和定义 RESTful API
- 在jsp中使用EXTJS的时候老提示EXT未定义
- IOS OC声明变量在@interface括号中与使用@property的区别
- Xcode8.x使用CoreData模型出现类被非法重定义的解决办法
- SP2010开发和VS2010专家"食谱"--第四章节—列表定义和内容类型(3)--使用对象模型创建自定义内容类型
- 使用flags定义命令行参数
- 定义使用函数指针
- PL/SQL函数的定义与使用