接口自身可从多个接口继承
public interface Ia //接口Ia声明,接口修饰符可以是new、public、protected、internal和private,接口的命名建议以大写字母I开头
{
void mymethod1();
}
public interface Ib //接口Ib声明
{
int mymethod2(int x);
}
public interface Ic : Ia, Ib //接口Ic从Ia和Ib继承
{
}
则Ic拥有Ia的method1方法和Ib的method2方法。
接口
interface Ia //声明接口Ia
{
float getarea(); //接口成员声明
}
interface Ib
{
float getarea2();
}
public class Rectangle : Ia,Ib //类Rectangle继承接口Ia
{
float x, y;
public Rectangle(float x1, float y1) //构造函数
{
x = x1; y = y1;
}
float Ia.getarea() //显式接口成员实现,带有接口名前缀,不能使用public
{
return x * y;
}
float Ib.getarea2()
{
return x * x * y * y;
}
}
Rectangle box1 = new Rectangle(2.5f, 3.0f);//定义一个类实例
Ia ia = (Ia)box1; //定义一个接口实例
Console.WriteLine("长方形面积: {0}", ia.getarea());
Rectangle box2 = new Rectangle(1.5f, 2.0f);//定义一个类实例
Ib ib = (Ib)box2; //定义一个接口实例
Console.WriteLine("长方形面积: {0}", ib.getarea2());