接口相当于是一种规范,是一种规定。比如规定内存条的长度、电压、缺口位置等这种规定,就是接口,而内存条按照这种规范被生产出来,就是实现接口规范的类。而电脑可以使用任何按照这种规范被生产出来的内存条,就体现了多态。
接口:
接口是一种规范协议;约定好遵守某种规范就可以写通用的代码。
定义了一组具有各种功能的方法。(只是一种能力,没有具体实现,就像抽象方法,“光说不做”)。
1. 接口存在的意义: 多态。
多态的意义:程序的可扩展性,最终让程序变得更加灵活,节省成本,提高效率。
2. 接口的特性:
接口可以实现“多继承”(多实现),一个类只能继承自一个父类,但是可以实现多个接口。
接口解决了不同类型之间的多态问题。
namespace 接口
{
class Program
{
static void Main(string[] args)
{
IFlyable fly = new MyClass();
//实现多态
fly.SayHi();
}
}
//接口不能被实例化
//接口就是让子类来实现的
//接口可以实现“多继承”(多实现),一个类只能继承自一个父类,但是可以实现多个接口。
//接口解决了不同类型之间的多态问题。
//定义一个接口,建议:一定要以大写I开头
public interface IFlyable
{
//接口里面只能包含方法。
//方法、属性、索引器、事件 ----“方法”
//接口中的所有成员都不能显示的写任何访问修饰符
//默认是public的访问修饰符
//接口中不能有任何的实现。
void SayHi();
void M1(string msg);
//定义一个属性
//在接口中这样写表示是一个未实现的属性
string name
{
get;
set;
}
//定义一个索引器
string this[int index]
{
get;
set;
}
}
//接口中的成员子类必须实现
public class MyClass:IFlyable
{
public void SayHi()
{
throw new NotImplementedException();
}
public void M1(string msg)
{
throw new NotImplementedException();
}
public string name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public string this[int index]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
}
案例1.(继承了一个类,同时实现了其他接口)
namespace 接口
{
class Program
{
static void Main(string[] args)
{
IFlyable bird = new Sparrow();
bird.Fly();
Console.ReadKey();
}
}
public interface IFlyable
{
void Fly();
}
//写一个“鸟”的父类
public class Bird
{
static void Bark()
{
Console.WriteLine("叫》》》》》");
}
}
//当一个类同时继承父类,并且实现了多个接口的时候,必须将继承类,写在第一个。
//麻雀
public class Sparrow : Bird, IFlyable
{
//实现接口的成员函数
public void Fly()
{
Console.WriteLine("麻雀飞上天");
}
}
//鸵鸟
public class Ostrich:Bird
{
}
//企鹅
public class Penguin : Bird
{
}
//鹦鹉
public class Parrot : Bird,IFlyable
{
//实现接口的成员函数
public void Fly()
{
Console.WriteLine("鹦鹉飞");
}
}
}
案例2.
namespace 接口
{
class Program
{
static void Main(string[] args)
{
ICollectHomeWork person = new Student();
person.Collect();
Console.ReadKey();
}
}
//接口
public interface ICollectHomeWork
{
void Collect();
}
public class Person
{
}
public class Student:Person,ICollectHomeWork
{
//实现接口中的函数
public void Collect()
{
Console.WriteLine("学生收作业");
}
}
public class Teacher : Person,ICollectHomeWork
{
public void Collect()
{
Console.WriteLine("老师收作业");
}
}
public class SchoolMaster : Person
{
}
}
3. 显示实现接口
显示实现接口是为了解决方法重名点的问题。
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//Student stu = new Student();
//stu.Collect();
//Console.WriteLine();
Teacher tea = new Teacher();
//因为显示实现接口时私有的,因此不能在此处访问到
tea.Collect(); //正常实现的Collect方法,显示实现的Collect方法调不到,因为是private
IFace1 face1 = new Teacher();
face1.Collect();
//通过接口可以调到该方法,因为接口中的方法都是公有的。
IFace2 face2 = new Teacher();
face2.Collect();
Console.ReadKey();
}
}
//接口
public interface IFace1
{
void Collect();
}
public interface IFace2
{
void Collect();
}
public class Person
{
}
public class Student : Person, IFace1,IFace2
{
//实现接口函数
public void Collect()
{
Console.WriteLine("实现了IFace1接口中的Collect方法");
}
}
public class Teacher : Person, IFace1,IFace2
{
//实现接口函数
public void Collect()
{
Console.WriteLine("实现了IFace11111接口中的Collect方法");
}
//显示实现接口
//私有成员
//显示实现接口没有访问修饰符,在方法名称前加了“接口名”,形如: 接口名.方法名
void IFace2.Collect()
{
Console.WriteLine("实现了IFace22222接口中的Collect方法");
}
}
}