1接口中只包含方法,属性,事件,索引器的签名不能包含实现,接口中不能包含字段,常量,运算符,实例构造函数,析构函数,和任何静态成员。
接口的成员默认是public的且不可再显示声明为public的,接口的声明实际上是抽象的
属性和索引器的签名必须包含完整的get 和set访问器
可以再基类中实现作为接口的实现,在派生类中不需要再次实现接口中的为实现成员,可以直接使用派生类像上造型为接口引用,然后使用接口引用调用接口在基类中实现的为实现成员。使用格式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceTest
{
class Program
{
static void Main(string[] args)
{
ISample sample = new ClassTwo();
sample.Method1();
}
}
interface ISample
{
void Method1();
}
class ClassOne
{
public void Method1()
{
Console.WriteLine("from classone Isample.method1()");
}
}
class ClassTwo : ClassOne,ISample
{
//no method to implements
}
}
2
实现多个接口后;要想使用对应 接口中的方法时,必须将实例向上造型到实现接口的引用后使用接口的引用去调用实现类中的方法。向上造型到指定的引用后,在 该接口中未定义的成员都是不可见的。
3
实现具有相同成员的接口时可以只提供一种实现,以供所有提供了该成员的接口共享,也可以显示的定义接口成员。显示的成员接口实现时使用方法显得名就可以。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XSinterfaceTest
{
class Program
{
static void Main(string[] args)
{
ClassOne clsone = new ClassOne();
ISample isample1=(ISample)clsone;
ISample2 isample2 = (ISample2)clsone;
isample1.Method1();
isample2.Method1();
}
}
interface ISample
{
void Method1();
}
interface ISample2
{
void Method1();
}
class ClassOne : ISample, ISample2
{
void ISample.Method1()
{
Console.WriteLine(" from classOne .isample.method1()");
}
void ISample2.Method1()
{
Console.WriteLine("from classOne.isample2.method1()");
}
}
}
使用限定接口名称的方式实现多实现接口中的相同方法签名的实现时,实现方法不允许带任何的访问修饰符,public也不可以,但是如果是普通是接口实现时接口的定义中不允许带任何的访问修饰符默认是public的,但是在实现类中必须添加 public修饰符,并且不可以省略。
4
对于显示的接口实现只能通过接口进行访问,除此以外不能用任何方式访问,即使在实现类的其他方法中也不能访问。在类的方法中调用接口中实现方法的形式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XSinterfaceTest
{
class Program
{
static void Main(string[] args)
{
ClassOne clsone = new ClassOne();
clsone.NewMethod();
}
}
interface ISample
{
void Method1();
}
interface ISample2
{
void Method1();
}
class ClassOne : ISample, ISample2
{
void ISample.Method1()
{
Console.WriteLine(" from classOne .isample.method1()");
}
void ISample2.Method1()
{
Console.WriteLine("from classOne.isample2.method1()");
}
public void NewMethod()
{
((ISample2)this).Method1();
((ISample)this).Method1();
}
}
}
在这种情况下,实现类的 派生类也无法访问显示的接口成员实现,只能通过接口的引用进行访问调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XSinterfaceTest
{
class Program
{
static void Main(string[] args)
{
ClassTwo clsTwo = new ClassTwo();
clsTwo.Method3();
}
}
interface ISample
{
void Method1();
}
interface ISample2
{
void Method1();
}
class ClassOne : ISample, ISample2
{
void ISample.Method1()
{
Console.WriteLine(" from classOne .isample.method1()");
}
void ISample2.Method1()
{
Console.WriteLine("from classOne.isample2.method1()");
}
public void NewMethod()
{
((ISample2)this).Method1();
((ISample)this).Method1();
}
}
class ClassTwo : ClassOne
{
public void Method3()
{
((ISample2)this).Method1();
((ISample)this).Method1();
}
}
}
5
接口可以多继承,但是类只能单继承,接口和类都可以实现多个接口,接口继承基接口后,基接口中的成员对该接口是可见的,在引用该接口时可以访问到基接口中的成员。