您的位置:首页 > 编程语言 > C#

c# 接口(interface)与接口应用

2013-07-21 01:46 274 查看
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[5];
emp[0] = new Manager("张三");
emp[1] = new Secretary("李四");
emp[2] = new Seller("王五");
emp[3] = new Seller("马六");
emp[4] = 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[5];
arr[0] = new Student("张三", 5);
arr[1] = new Student("李四", 3);
arr[2] = new Student("王五", 2);
arr[3] = new Student("马六", 4);
arr[4] = new Student("钱七", 1);
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: