c# 接口(interface)与接口应用
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)与接口应用的更多相关文章
- 接口interface,接口继承implements
php中,只支持从一个类继承,不支持从两个或者更多的类同时继承.从两个或者两个以上的类继承的能力被称为多重继承.php在设计上是禁止这种功能的.原因在于,避免多个类带来的复杂性.当发现需要从两个或者更 ...
- C#编程利器之三:接口(Interface)【转】
C#编程利器之三:接口(Interface) C#接口是一个让很多初学者容易迷糊的东西,用起来好象很简单,定义接口,然后在里面定义方法,通过继承与他的子类来完成具体的实现.但没有真正认识接口的作用的时 ...
- delphi 接口Interface
学习 delphi 接口 一切都是纸老虎!!! 第四章 接口 前不久,有位搞软件的朋友给我出了个谜语.谜面是“相亲”,让我猜一软件术语.我大约想了一分钟,猜 出谜底是“面向对象”.我 ...
- 接口--interface
“interface”(接口)关键字使抽象的概念更深入了一层.我们可将其想象为一个“纯”抽象类.它允许创建者规定一个类的基本形式:方法名.自变量列表以及返回类型,但不规定方法主体.接口也包含了基本数据 ...
- Golang接口(interface)三个特性(译文)
The Laws of Reflection 原文地址 第一次翻译文章,请各路人士多多指教! 类型和接口 因为映射建设在类型的基础之上,首先我们对类型进行全新的介绍. go是一个静态性语言,每个变量都 ...
- java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0
java 继承使用关键字extends 继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...
- java中的接口interface
关于接口 接口描述了实现了它的类拥有什么功能.因为Java是强类型的,所以有些操作必须用接口去约束和标记.接口作为类的能力的证明,它表明了实现了接口的类能做什么. 类似与class,interface ...
- php中的抽象类(abstract class)和接口(interface)
一. 抽象类abstract class 1 .抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 2 ...
- [No000035]操作系统Operating System之OS Interface操作系统接口
接口(Interface) 仍然从常识开始… 日常生活中有很多接口:电源插座:汽车油门… 那什么是接口? 连接两个东西.信号转换.屏蔽细节… Interface: electrical circuit ...
- 14 接口-interface的定义与实现
接口的基本语法一: 1.使用interface 定义 2.接口当中的方法都是抽象方法 3.接口当中的方法都是public权限 接口的定义: interface USB { public void re ...
随机推荐
- Java对象类型的判断
instanceof 判断某个对象是否是某个类的实例或者某个类的子类的实例.它的判断方式大概是这样的: public<T> boolean function(Object obj, Cla ...
- FreeBSD中实现root的ssh
在虚拟机中刚装好FreeBSD,结果不能以远程方式登录root,然后在如下链接中找到了解决方法: http://blog.chinaunix.net/uid-26719405-id-3822697.h ...
- Scrapy爬虫入门系列3 将抓取到的数据存入数据库与验证数据有效性
抓取到的item 会被发送到Item Pipeline进行处理 Item Pipeline常用于 cleansing HTML data validating scraped data (checki ...
- LOCAL_SHARED_LIBRARIES 与 LOCAL_LDLIBS,LOCAL_LDFLAGS的区别
LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到.且貌似只能链接那些存在于系统目录下本模块需要连接的库.如果某一个库既有动态库又有静态库,那么 ...
- important——》sql server 2000安装图解
MS Sql Server 2000包含两个部分:服务器组件和客户端工具,其中服务器组建是以Windows服务的方式运行的,有四种服务分别是:MSSqlServer.Distributed Trans ...
- Unity3D学习笔记——IDE工作视图
Unity3D中五个界面的使用: Project视图:存放游戏资源,比如贴图,音频,JS脚本等 Project中可创建的文件如下: Hierarchy视图:主要存放游戏场景中的对象,如摄像机,精灵,箱 ...
- JavaScript API 设计准则
好的 API 设计:在自描述的同时,达到抽象的目标. 设计良好的 API ,开发者可以快速上手,没必要经常抱着手册和文档,也没必要频繁光顾技术支持社区. 流畅的接口 方法链:流畅易读,更易理解 //常 ...
- 第二章----python函数
第一节:调用函数 1.函数是什么? 函数是组织好的,可以重复利用的. 2.为什么要用到函数? 提高应用的模块性,提高重复利用率.指的是:多个文件中可能都要用到该函数,直接拿来调用就行,不用在重复写一个 ...
- jprofiler_监控远程linux服务器的JVM进程(转 非常棒)
几天前写了一篇文章,jprofiler_监控远程linux服务器的tomcat进程(实践),介绍了使用jprofiler怎样监控远程linux的tomcat进程,这两天想了想,除了可以监控tomcat ...
- 6、easyUI-拖放事件及应用
一.EasyUI 基本的拖动和放置 直接代码看: <!doctype html> <html> <head> <meta http-equiv="C ...