接口只声明、无实现、不能实例化;
接口可包含方法、属性、事件、索引器, 但无字段;
接口成员都是隐式的 public, 不要使用访问修饰符;
类、结构和接口都可以继承多个接口;
继承接口的类必须实现接口成员, 除非是抽象类;
类实现的接口成员须是公共的、非静态的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//入户门式列
interface MyInterface
{
int Sqr(int x);
}
class MyClass:MyInterface
{
public int Sqr(int x)
{
return x * x;
}
}
interface ImyInterface
{
int Method(int x, int y);
}
class MyClass1:ImyInterface
{
public int Method(int x,int y)
本文通过实例介绍了C#中的接口使用,包括声明接口、类实现接口以及多接口继承。示例中展示了如何定义并实现带有方法的接口,以及如何通过接口调用方法。同时,还探讨了接口的隐式实现和显示实现的差异。
订阅专栏 解锁全文
1693

被折叠的 条评论
为什么被折叠?



