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. DJI SDK iOS 开发之二:搭建主要的开发环境

    本文想介绍搭建主要的DJI SDK iOS下的开发环境,只是DJI官方已经给出了非常具体的执行其demo的教程.网址例如以下: https://dev.dji.com/cn/guide 我这里总结一下 ...

  2. web开发中经常使用的js

    将自己在web开发中经经常使用到的一些JS总结一下. 1.改动标签和表单的值 改动标签的值: var customer = document.getElementById("custm&qu ...

  3. Fibonacci series(斐波纳契数列)的几种常见实现方式

    费波那契数列的定义: 费波那契数列(意大利语:Successione di Fibonacci),又译费波拿契数.斐波那契数列.斐波那契数列.黄金切割数列. 在数学上,费波那契数列是以递归的方法来定义 ...

  4. Android Otto框架浅析

    今天要介绍的是一个Android中使用得比較多的android 事件总线 EventBus模式的一个框架Otto. Otto 官网:http://square.github.io/otto/ 一.An ...

  5. android Material design是什么

    Material design概述: Material design是一套UI样式标准,应该会提供一些新的API这写API包含了以下五大模块内容,分别是: Material Theme New Wid ...

  6. python3----练习题(三级菜单)

    三级菜单程序. 运行程序输出第一级菜单. 选择一级菜单某项,输出二级菜单,同理输出三级菜单. 退出时返回上一级菜单 menu = { '北京市': { '东城区': { 'aa', 'bb', }, ...

  7. caffe net 可视化工具,,层特征可视化

    1.只用网络在线结构绘制可视化网络模型 http://ethereon.github.io/netscope/#/editor 将对应的网络输入到里面,然后按shift+enter即可查看对应的网络结 ...

  8. orm查询基本操作

    orm  ----关系对象模型 orm 的查询两种操作 1.基于对象的查询 oneTo one 一对多 和多对多 对象.关联字段 对象.表名(_set)) 2.基于QUERYset的查询 双下划线即为 ...

  9. 页面加载,使用ajax查询某个类别,并且给它们添加(拼接)连接

    直接上代码 $(function(){ var a = ''; $.get("productType1_findAll.action",function(data){ consol ...

  10. PowerDesigner如何导出表到word的方法

    from:https://jingyan.baidu.com/article/295430f1c385970c7f005056.html PowerDesigner如何导出表到word的方法 听语音 ...