接口的概念:简单地说借口就是定义了一些方法的类,但这些方法并未实现。接口可以多重继承,一个类一旦引用了一个或多个接口,就必须实现,否则是报错。
定义接口:
namespace 接口
{
interface Interface1
{
int getmax(int a,int b);
void reset(string n);
}
}
//类class实现接口interface1
class Class1 : Interface1
{
int s;
//实现接口的方法必须声明为public,因为有些方法在接口中是隐式共有的,所以实现也必须是共有的
public int getmax(int m, int n
{
if (m > n)
{
s = m;
return s;
}
else
{
s = n;
return s;
}
}
public void reset(string s)
{
Console.WriteLine(s);
}
}
//main方法
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
Console.WriteLine( c.getmax(10,6));
c.reset("hello world");
Console.ReadLine();
}
}
运行结果如下:
10
hello world