zoukankan
html css js c++ java
C#接口--C#基础
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)显示实现接口
查看全文
相关阅读:
使用axi_datamover完成ZYNQ片内PS与PL间的数据传输
ZYNQ 的PS GEM DMA存在缺陷
异构数据源离线同步工具
58同城2015校招笔试、一面、二面经历
深圳科陆集团2015校招软件开发笔试题
华为2015校园招聘研发面试总结(获得offer)
2015校园招聘360失败的惨痛经历
数码视讯2015校园招聘JAVA笔试题及答案
百度2015校园招聘一、二、三面面试经历(软件研发岗)
2014美团网校园招聘研发类笔试(哈尔滨站)
原文地址:https://www.cnblogs.com/qikeyishu/p/7504094.html
最新文章
地图平衡性的困惑
埃洛等级分系统【转自百度百科】
计算玩家的游戏生命周期时的一些想法
js随笔--关于数组
JSON.stringify() 与JSON.parse()
运算符基础知识——比较运算符
spring boot 利用切面和反射进行自动关联查询
struts2 ajax文件上传类型拦截后的消息提示
Ubuntu下MongoDB安装&使用NodeJS操作MongoDB
树莓派 GrovePi Raspberry Pi 3B 添加 Grove Pi 扩展
热门文章
Eclipse 安装 svn
Linux Ubuntu 下 apache 配置
test
论文笔记(2):A fast learning algorithm for deep belief nets.
麦子学院学习视频之机器学习(1):1.2深度学习介绍
麦子学院学习视频之机器学习(1):1.1 机器学习介绍
论文笔记(1):Deep Learning.
Linux下Zabbix5.0 LTS监控基础原理及安装部署(图文教程)
Tesla P4 在深度学习上的性价比辗压目前所有量产的FPGA
ZYNQ fsbl阶段的调试方法
Copyright © 2011-2022 走看看