1、接口的声明

接口:描述属于任何类或者结构的一组相关功能,是一种规范、功能

组成:属性、方法、事件、索引或者这四种成员的任意组合构成

基本知识点:

1)接口默认的权限修饰符是:public,不允许加权限修饰符【如:interface IEatable{},不能是public interface IEatable{}】,成员也不能加abstract【正确是:string Name{get;set};public string Name{get;set;}是错的】

2)不能有字段:public  string  _name;是错的【首先修饰符不能加,默认是public,string _name违反了不能有字段】

3)接口中不能有字段,所以属性被写作自动属性,因为无法操作字段:

string Name{

get;

set;

}

4)不允许写实现方法体的函数: void Write(); 不能是:

void Write(){

..............;

}

方法的返回值不受影响。

5)实现过程必须在实现接口中的类中完成。

6)接口命名:接口一般以大写字母I开头,跟在I后面也是大写字母,结尾able 如:IFlyable

实例相关代码笔记:

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
//public interface IFlyable
//{
//实际不这样做
//}
class Program//类默认private
{
static void Main(string[] args)
{

}
}
}

IEatable.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口
{
interface IEAtable//接口默认是public
{
string Name//接口中不能有访问修饰符 如:public string Name错的
{
get;
set;//无字段,写成自动属性,无法操作字段
}
//接口不能有字段,所以属性经常(必须)被写作自动属性
void Write();//不能包含方法体
string Read();//返回值不受影响
//string _name;//不能有访问修饰符,不能有字段,所以不能用
//接口成员不允许添加访问修饰符默认是public不能加abstract
//实现过程必须在实现接口的类中完成
}
}

2、接口的实现和继承

1)类继承接口具有单继承,而接口继承接口是多重继承

2)接口多重继承时,派生接口名与父接口用冒号隔开,多个父接口用逗号隔开;父接口也称为该接口的显示基接口。

单一继承:

namespace 接口实现
{
interface Interface4:Interface3
{
}
}

多重继承:

namespace 接口实现
{
interface Interface3:Interface1,Interface2
{
}
}

2)不同的接口(不包含派生)中允许有同名的成员

interface Interface1
{
void read();
}

interface Interface2
{
void read();
}

3)同一接口中成员名不能重名,即使类型(一个是属性、一个是方法)名不同

namespace 接口实现
{
interface Interface2
{
void read();
string read
{
get;
set;
} //错误的
//同一接口中成员名不能重名,即使类型(一个是属性,一个是方法)不同
//不同的接口(不包含派生)中允许有同名的成员
}
}

4)如果在派生接口中对显示基接口中的成员进行重定义,需要用new 关键字解除警告。

interface Interface1
{
void read();
}

interface Interface2
{
void write();
}

interface Interface3:Interface1,Interface2
{
new void write();//void write();没有错误,但会有警告
}

5)为什么用到接口?开放封闭原则:软件实体应该可以扩展,但不可以修改。对扩展是开放的,对修改是是封闭的,封闭就是不可以操纵。

举例示例:鸵鸟、麻雀、老鹰都是鸟,鸟作为父类供子类继承

class Bird
{
public void write(){
Console.WriteLine("我是鸟类!");
}
}

class Ostrich:Bird
{
public void write()
{
Console.WriteLine("我是鸵鸟,我吃青草!");
}
}

class Eagle : Bird
{
public void Eat() {
Console.WriteLine("我是老鹰,吃小鸡");
}
}

class Sparrow : Bird
{
public void write()
{
Console.WriteLine("我是麻雀!");
}
}

如果要加实现“飞”的功能,但鸵鸟不会飞,解决方案:

1>在父类中加飞的方法,它不会飞,但鸵鸟继承了父类,方案不可选。

2>在子类中加飞的方法,谁会飞就加飞的方法,完全可以,但违背开放封闭原则。如果分别在子类中去实现飞的方法,在子类中就可以进行修改,但软件实体不能扩展,每新增一个会飞的内容就需要在子类中修改。

3>上例中如果新增一个气球、飞机,它们继承鸟(飞的方法)的父类就不合适。然而接口可以,提供了会飞的方法,麻雀需要会飞,就实现接口;鸵鸟不会就不需要继承。

6)接口的实现

IFlyable.cs:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
interface IFlyable
{
void Fly();
}
}

Bird.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
abstract class Bird
{
public abstract void Eat();
//public void write(){
//Console.WriteLine("我是鸟类!");
//}
}
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Program
{
static void Main(string[] args)
{
IFlyable[] fly={new Sparrow(),new Eagle(),new Swan(),new Balloon()};//接口类型的数组
foreach(IFlyable outFly in fly)
outFly.Fly();
Console.ReadKey();
}
}
}

Ostrich.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Ostrich:Bird
{
public override void Eat()
{
Console.WriteLine("我是鸵鸟,我吃青草!");
}
}
}

Eagle.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Eagle : Bird,IFlyable
{
public void Fly() {
Console.WriteLine("我是老鹰,会飞");
}
public override void Eat() {
Console.WriteLine("我是老鹰,吃小鸡");
}
}
}

Sparrow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Sparrow : Bird,IFlyable
{
//接口的实现需要在实现接口的类中进行,重写下
public void Fly()
{
Console.WriteLine("我是麻雀,我会飞");
}
public override void Eat()
{
Console.WriteLine("我是麻雀!吃粮食");
}
}
}

Balloon.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Balloon:IFlyable
{
public void Fly() {

Console.WriteLine("我是气球,我也会飞");
}
}
}

Swan.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 接口实现
{
class Swan:Bird,IFlyable
{
public override void Eat() {
Console.WriteLine("我是天鹅,我吃鱼");
}
public void Fly() {

Console.WriteLine("我是天鹅,我会飞" );
}
}
}

运行结果:

--7)显示实现接口

C#接口--C#基础相关推荐

  1. WLGK-51单片机接口技术基础实验 —LED闪烁灯

    WLGK-51单片机接口技术基础实验--LED闪烁灯 当我们开始接触单片机,首先接触的第一个实验就是LED灯的使用,类似于我们学习软件开始接触的第一个程序"HelloWorld", ...

  2. WLGK-51单片机接口技术基础实验 ——LED闪烁灯

    WLGK-51单片机接口技术基础实验-LED闪烁灯 当我们开始接触单片机,首先接触的第一个实验就是LED灯的使用,类似于我们学习软件开始接触的第一个程序"HelloWorld",这 ...

  3. 《嵌入式系统原理与接口技术》——嵌入式系统接口应用基础

    本文为我负责编写的电子工业出版社出版的<嵌入式系统原理与接口技术>一书第七章部分,这里整理的仍然是修改稿,供需要的同学参考,本书为普通高等教育"十二五"规划教材,电子信 ...

  4. 微型计算机接口及基础,微型计算机技术与接口应用基础

    微型计算机技术与接口应用基础 语音 编辑 锁定 讨论 上传视频 <微型计算机技术与接口应用基础>是2011年清华大学出版社出版的一本图书[1] 书    名 微型计算机技术与接口应用基础 ...

  5. Java学习(9)(3种向上转型的方式、重写、向下转型、多态的优缺点、抽象类【基础规则、抽象类的作用】接口【基础规则、 接口的使用】)

    接上次博客:JAVA学习(8)继承 ( 继承的注意事项.Java的执行顺序.继承方式.限定词protcted.final关键词.多态.动态绑定和静态绑定 )_di-Dora的博客-CSDN博客 目录 ...

  6. python接口自动化基础框架结构 ——分层

    python接口自动化基础框架结构 --分层 --bin 用于存放启动文件,如run.py --cases cases目录,存放测试脚本 --data   YAML文件 --lib  存放各种附加的代 ...

  7. [ACPI高级配置与电源接口] -ACPI基础

    [ACPI高级配置与电源接口] -ACPI基础 什么是ACPI Advanced Configuration and Power Interface 由1997年,英特尔.微软.东芝公司共同提出.制定 ...

  8. 接口的基础语法和接口在开发中的作用2021-07-23java学习日记

    Javase进阶 接口的语法和接口的作用 p499-p516 关键词: 接口    implements    面向抽象编程    OCP开闭原则 接口的基础语法 1.接口也是一种引用数据类型,编译之 ...

  9. db9针232接口波特率标准_RS232和RS485与RS422接口的基础知识详细介绍

    一.RS232基础知识计算机 与计算机或计算机与终端之间的数据传送可以采用串行通讯和并行通讯二种方式.由于串行通讯方式具有使用线路少.成本低,特别是在远程传输时,避免了多条线路特性的不一致而被广泛采用 ...

  10. day15 java接口的基础语法

    1.基础语法 2.接口多继承 3.类和接口多实现 3.1实现没有继承关系会怎么样 4.继承和实现都存在 5.接口基础语法总结

最新文章

  1. Yolo-FastestV2 移动端可达300FPS,参数量仅250k
  2. python画树叶-使用Python turtle画分形树叶图
  3. java如何把查到的对象集合放入 展示对象list中_Java面试整理-基础篇8.集合1
  4. 选择结构_扩展if-else语句
  5. ElasticSearch(一)基础知识
  6. Spring 源码分析之AbstractApplicationContext源码分析
  7. 平台框架_从框架到平台
  8. Flask 第三方组件之 login
  9. Mint-UI 报错提示缺少“raf.js / vue-lazyload / vue-popup” - 解决办法
  10. scrapy splash 爬取图片学习心得
  11. osm数据下载 python_用Python编写小工具下载OSM路网数据
  12. js实现二级联动菜单
  13. 自动化测试用例设计方法
  14. 200道常见java知识点总结,问题+答案(转)
  15. EHCI主机控制器--周期帧列表(periodic frame list)
  16. 13. 中国古代数学家张丘建在他的《算经》中提出了一个著名的“百钱百鸡问题”:一只公鸡值5钱,一只母鸡值3钱,三只小鸡值1钱,现在要用百钱买百鸡,请问公鸡、母鸡、小鸡各多少只?
  17. JVM内置函数intrinsics简介
  18. 手机中好用的软件有哪些?让我来告诉你吧
  19. 新华三网络部署(和思科的区别)
  20. 客户体验和营销:您需要了解的 5 个最佳实践

热门文章

  1. Vert.x ——概述
  2. MySQL环境变量的配置(三)(Windows 11)
  3. 牛津英语字典pdf下载_除了long time no see,你知道还有这些中式英语也进入了牛津字典吗...
  4. 常用IP地址端口对照表
  5. Java实现-五子棋
  6. mapping.xml POJO的映射文件
  7. 全国省份地区相关常量备忘
  8. NVIDIA 为微软 Xbox One 游戏机发布 PhysX 支持
  9. C#实现串口通信的上位机开发
  10. 4.24、半关闭、端口复用