using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//接口(interface)
//接口(interface)定义了一个可由类和结构实现的协定。接口可以包含方法、属性、事件和索引器。
//接口不提供它所定义的成员的实现——它仅指定实现该接口的类或结构必须提供的成员。
//
//接口
//1、一个接口声明可以声明零个或多个成员。
//2、接口的成员必须是方法、属性、时间或索引器。
//3、接口不能包含常量、字段、运算符、实例构造函数、析构函数或类型,也不能包含任何种类的静态成员。
//4、所有接口成员都隐式地具有Public访问属性。
//5、接口成员声明中包含任何修饰符都属于编译时错误。具体来说,不能使用修饰符abstract、public、protected、
//internal、private、virtual、override或static来声明接口成员。
namespace 接口
{
//定义一个接口
interface IDrivingLicenceB
{
void GetLicence();
}
//IDrivingLicenceA接口继承与IDrivingLicenceB
interface IDrivingLicenceA : IDrivingLicenceB
{
new void GetLicence();
}
//老师类
class Teacher : IDrivingLicenceA
{
void IDrivingLicenceB.GetLicence()
{
Console.WriteLine("老师获得了B类驾驶执照");
}
void IDrivingLicenceA.GetLicence()
{
Console.WriteLine("老师获得了A类驾驶执照");
}
public void GetLicence()
{
Console.WriteLine("这个不是接口的方法");
}
}
//学生类
class Student : IDrivingLicenceB
{
void IDrivingLicenceB.GetLicence()
{
Console.WriteLine("学生获得了B类驾驶执照");
}
public void GetLicence()
{
Console.WriteLine("这个不是接口的方法");
}
}
class Program
{
static void DriveCar(string name, IDrivingLicenceB o)
{
IDrivingLicenceB d1 = o as IDrivingLicenceB;
if (d1 != null) //实现了IDrivingLicenceB接口的情况
{
d1.GetLicence();
Console.WriteLine(name + "开动了卡车");
}
else
{
Console.WriteLine(name + "没有驾驶执照,不能开卡车");
}
}
static void DriveBus(string name, IDrivingLicenceB o)
{
IDrivingLicenceA d1 = o as IDrivingLicenceA;
if (d1 != null) //实现了IDrivingLicenceA接口的情况
{
d1.GetLicence();
Console.WriteLine(name + "开动了公共汽车");
}
else
{
Console.WriteLine(name + "没有驾驶执照,不能开公共汽车");
}
}
static void Main(string[] args)
{
Teacher t = new Teacher();
DriveCar("教师", t);
DriveBus("教师", t);
Student s = new Student();
DriveCar("学生", s);
DriveBus("学生", s);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 接口的多继承
{
interface IA
{
void F();
}
interface IB : IA
{
new void F();
}
interface IC : IA
{
void G();
}
interface IBC : IB, IC
{ }
class Derive : IBC
{
public void F()
{
Console.WriteLine("IB.F()");
}
public void G()
{
Console.WriteLine("IC.G()");
}
}
class Program
{
static void Main(string[] args)
{
Derive d = new Derive();
((IA)d).F(); //"IB.F()"
((IB)d).F(); //"IB.F()"
((IC)d).F(); //"IB.F()"
((IBC)d).F(); //"IB.F()"
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//接口重新实现
//一个类若继承了某个接口的实现,则只要将该接口列入它的基类列表中,就可以重新实现(re-implement)该接口。
//接口的重新实现与接口的初始实现遵循完全相同的接口映射规则。因此,继承的接口映射不会对为重新实现该接口而
//建立的接口映射产生任何影响。
namespace 接口的多继承1
{
interface IA
{
void F();
}
class B : IA
{
void IA.F()
{
Console.WriteLine("B.F()");
}
}
class C : B, IA
{
//void IA.F()
//{
// Console.WriteLine("C.B.F()");
//}
public void F()
{
Console.WriteLine("C.F()");
}
}
class Program
{
static void Main(string[] args)
{
C c = new C();
((IA)c).F(); //"C.F()"
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//接口与抽象类
//1、抽象类用于部分实现一个类,再由用户按需求对其进行不同的扩展和完善;接口只是定义一个行为的规范或规定。
//2、抽象类在组件的所有实现间提供通用的已实现功能;接口创建在大范围全异对象间使用的功能。
//3、抽象类主要用于关系密切的对象;而接口适合为不相关提供通用功能。
//4、抽象类主要用于设计大的功能单元;而接口用于设计小而简练的功能块。
namespace 接口的多态
{
//员工类
interface IEmployee
{
void StartWork();
}
//经理类
class Manager : IEmployee
{
private string _name;
public Manager(string name)
{
_name = name;
}
public void StartWork()
{
Console.WriteLine(_name + "给员工下达任务");
}
}
//秘书类
class Secretary : IEmployee
{
private string _name;
public Secretary(string name)
{
_name = name;
}
public void StartWork()
{
Console.WriteLine(_name + "协助经理");
}
}
//销售类
class Seller : IEmployee
{
private string _name;
public Seller(string name)
{
_name = name;
}
public void StartWork()
{
Console.WriteLine(_name + "销售产品");
}
}
//财务类
class Accountant : IEmployee
{
private string _name;
public Accountant(string name)
{
_name = name;
}
public void StartWork()
{
Console.WriteLine(_name + "管理公司财政");
}
}
class Program
{
static void Main(string[] args)
{
IEmployee[] emp = new IEmployee[];
emp[] = new Manager("张三");
emp[] = new Secretary("李四");
emp[] = new Seller("王五");
emp[] = new Seller("马六");
emp[] = new Accountant("钱七");
Console.WriteLine("早上8点,开始工作");
foreach (IEmployee e in emp)
{
e.StartWork();
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
//Array.Sort(Array array)使用IComparable接口CompareTo方法,实现对象类型排序。
//Array.Sort(Array array, IComparer comparer) 使用IComparer接口Compare方法,进行排序。
namespace 接口应用
{
class Student : IComparable
{
private string _name;
private int _age;
public Student(string name, int age)
{
_name = name;
_age = age;
} public string Name
{
get { return _name; }
set { _name = value; }
} public int Age
{
get { return _age; }
set { _age = value; }
}
int IComparable.CompareTo(object right)
{
if (!(right is Student))
{
throw new ArgumentException("参数必须为Student类型");
}
return _name.CompareTo(((Student)right)._name);
}
public int CompareTo(Student right)
{
return _name.CompareTo(right._name);
}
private static AgeIComparer _ageCom = null; //创建一个私有的静态的AgeIComparer类对象,并设置为空。 public static IComparer AgeCom //给_ageCom字段加上属性
{
get
{
if (_ageCom == null)
{
_ageCom = new AgeIComparer(); //实例化内部类
}
return _ageCom;
}
}
private class AgeIComparer : IComparer
{
int IComparer.Compare(object left, object right)
{
if (!(left is Student) || !(right is Student))
{
throw new AccessViolationException("参数必须为Student类型");
}
return ((Student)left)._age.CompareTo(((Student)right)._age); //根据年龄排序
}
} public override string ToString()
{
return _name + " " + _age;
}
}
class Program
{
static void Main(string[] args)
{
Student[] arr = new Student[];
arr[] = new Student("张三", );
arr[] = new Student("李四", );
arr[] = new Student("王五", );
arr[] = new Student("马六", );
arr[] = new Student("钱七", );
Console.WriteLine("对姓名进行排序");
Array.Sort(arr);
foreach (Student i in arr)
{
Console.WriteLine(i);
}
Console.WriteLine("对年龄进行排序");
Array.Sort(arr, Student.AgeCom);
foreach (Student i in arr)
{
Console.WriteLine(i.Name + " " + i.Age);
}
Console.ReadKey();
}
}
}

c# 接口(interface)与接口应用的更多相关文章

  1. 接口interface,接口继承implements

    php中,只支持从一个类继承,不支持从两个或者更多的类同时继承.从两个或者两个以上的类继承的能力被称为多重继承.php在设计上是禁止这种功能的.原因在于,避免多个类带来的复杂性.当发现需要从两个或者更 ...

  2. C#编程利器之三:接口(Interface)【转】

    C#编程利器之三:接口(Interface) C#接口是一个让很多初学者容易迷糊的东西,用起来好象很简单,定义接口,然后在里面定义方法,通过继承与他的子类来完成具体的实现.但没有真正认识接口的作用的时 ...

  3. delphi 接口Interface

    学习 delphi 接口 一切都是纸老虎!!! 第四章          接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我 ...

  4. 接口--interface

    “interface”(接口)关键字使抽象的概念更深入了一层.我们可将其想象为一个“纯”抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不规定方法主体.接口也包含了基本数据 ...

  5. Golang接口(interface)三个特性(译文)

    The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...

  6. java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0

    java 继承使用关键字extends   继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...

  7. java中的接口interface

    关于接口 接口描述了实现了它的类拥有什么功能.因为Java是强类型的,所以有些操作必须用接口去约束和标记.接口作为类的能力的证明,它表明了实现了接口的类能做什么. 类似与class,interface ...

  8. php中的抽象类(abstract class)和接口(interface)

    一. 抽象类abstract class 1 .抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 2 ...

  9. [No000035]操作系统Operating System之OS Interface操作系统接口

    接口(Interface) 仍然从常识开始… 日常生活中有很多接口:电源插座:汽车油门… 那什么是接口? 连接两个东西.信号转换.屏蔽细节… Interface: electrical circuit ...

  10. 14 接口-interface的定义与实现

    接口的基本语法一: 1.使用interface 定义 2.接口当中的方法都是抽象方法 3.接口当中的方法都是public权限 接口的定义: interface USB { public void re ...

随机推荐

  1. Cmder 配置使用

    官网下载 配置: 1.把 Cmder 加到环境变量 将Cmder.exe存放的目录添加到系统环境变量path 添加成功后,Win+r 输入cmder,可以正确打开cmder 窗口即可. 2.添加 cm ...

  2. 【转】支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  3. svn密码 在服务端 到底是明文保存,还是密文保存

    svn密码 在服务端 到底是明文保存,还是密文保存 之前在ubuntu上搭建svn服务端,记得密码就是明文保存的, 但最近项目,我们老大说,他搭建的是加密后的,我就比较纳闷了, 经过偷偷的瞄一下,不就 ...

  4. windows中控制台窗口和普通窗口有什么区别?

    1. 窗口都是windows标准窗口,有窗口句柄,但是console window没有消息循环,直接从缓冲区读数据,显示数据. windows中普通窗口都有自己的窗口过程, 我可以使用SetWindo ...

  5. JavaScript入门:004—JS凝视的写法和基本运算符

    JS的凝视JS中加凝视和寻常写C#代码是几乎相同的.有//和/* */这两种.单行凝视使用双斜杠比如. <script type="text/javascript"> ...

  6. Python:简述 线程、进程和协程

    Python线程 定义:Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- impor ...

  7. MySQL的having子句

    1.定义:having子句对分组的结果集进行进一步的筛选 2.语法:select 字段列表 from 表名称 [where 子句][gruop by 子句] [having 子句]; 3.举例:查询出 ...

  8. PHP 如何成长 (收藏自:http://www.cnblogs.com/try-better-tomorrow/p/6964036.html)

    原文题目为<php程序员学C/C++>,不过我觉得说是提升自己比较合适. 身边有几个做PHP开发的朋友,因为面试,也接触到不少的PHP工程师,他们常疑虑自己将来在技术上的成长与发展,我常给 ...

  9. Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.4——Flavor Dimensions

    问题: 一个product flavor不够,你需要另一个标准去区分不同版本的app 解决方案: 在product flavor中增加flavorDimensions 讨论: 在3.2章展示了一个有三 ...

  10. 索引笔记《一》Oracle中的索引详解

    一. ROWID的概念 存储了row在数据文件中的具体位置:64位 编码的数据,A-Z, a-z, 0-9, +, 和 /, row在数据块中的存储方式 SELECT ROWID, last_name ...