20-C#笔记-接口
# 1 接口的使用示例
使用interface,关键字
接口的实现和使用,和继承类似。
在使用之前,要实现接口。
using System;
interface IMyInterface
{
// 接口成员
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}# 2 接口的继承
在继承接口的类中,要实现所有的接口。
using System;
interface IParentInterface
{
void ParentInterfaceMethod();
}
interface IMyInterface : IParentInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
public void ParentInterfaceMethod()
{
Console.WriteLine("ParentInterfaceMethod() called.");
}
}参考:
http://www.runoob.com/csharp/csharp-interface.html
相关推荐
huimeiad 2020-11-23
往后余生 2020-09-17
hushijiao 2020-11-10
lxhuang 2020-11-03
Martian 2020-10-13
luguanyou 2020-10-05
夜影风个人空间 2020-09-22
哈嘿Blog 2020-09-08
0linker 2020-09-01
libaoshan 2020-09-11
saluzirobot 2020-09-01
txq0 2020-08-24
充满诗意的联盟 2020-08-23
jaybeat 2020-08-20
浪味仙 2020-08-17
ChinaJoeEE 2020-08-16
jbossrobbie 2020-08-16