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();
}
}
}

最新文章

  1. ES6箭头函数与展开运算符
  2. curl开源库编译
  3. 控制器View的生命周期及相关函数是什么?你在开发中是如何用的?
  4. strstr 函数用法
  5. 在csdn里markdown感受
  6. 使用sklearn进行数据预处理 —— 归一化/标准化/正则化
  7. 抽象类(abstract class)和 接口(interface)
  8. AFNetworking2.0后 进行Post请求
  9. .Net Framework基础知识
  10. 【vue系列之一】使用vue脚手架工具搭建vue-webpack项目
  11. IDEA Maven 三层架构 1、基本的Archetype 搭建
  12. June. 27th 2018, Week 26th. Wednesday
  13. exec sp_spaceused如何只返回一个结果集(转载)
  14. HTML5的manifest 本地离线缓存
  15. Python代码统计工具
  16. API 接口自动化测试框架
  17. ubantu-vim操作
  18. Squid安装配置和使用
  19. zabbix lld使用trapper方式(zabbix_sender)
  20. An application icon

热门文章

  1. spring 第一篇(1-2):管理你的beans
  2. VMware虚拟机 Ubuntu 实用技巧 (1) -- 安装VMware Tool
  3. Windows下使用python
  4. jQuery 属性操作方法
  5. python __new__ __init__ __del__
  6. c++中sin,cos,arcsin等和在C/C++中使用pi (π) 值
  7. poj 3469(网络流模版)
  8. Laravel5.1 路由 -路由分组
  9. 如何获取继承中泛型T的类型
  10. ABAP小白的成长日记--------helloblog