接口的写法

首先说接口的写法,其实写法很简单,跟C++的模板技术有点像,只不过方便类的管理和约束

class Program
    {
        static void Main(string[] args)
        {
            MyImage objm = new MyImage();
            objm.Open();
            IPict Pict = objm;  //IPict引用
            Pict.ApplyAlpha();
            IPictManip PcitManip = objm;
            PcitManip.ApplyAlpha();
            Console.ReadKey();
        }
    }
    public interface IPictManip
    {
        void ApplyAlpha();
    }
    public interface IPict
    {
        void ApplyAlpha();
    }
    public class BaselO
    {
        public void Open()
        {
            Console.WriteLine("BaselO的Open方法");
        }
    }
    public class MyImage : BaselO, IPict, IPictManip
    {
        void IPict.ApplyAlpha()
        {
            Console.WriteLine("实现了IPict的ApplyAlpha方法");
        }
        void IPictManip.ApplyAlpha()
        {
            Console.WriteLine("实现了IPictManip的ApplyAlpha方法");
        }
    }

注意接口必须是pubilc的,或者不写,默认页是public,当然接口本就是为了对外的,也不可能有私有的想法

接口的方法需要继承类重载去实现,调用接口,也需要具体的类进行实例化

感觉上很麻烦,但是其实就是说这几个类是一类的,无论是男程序猿,还是女程序猿,还是不伦不类的,都是程序猿

接口的契约形式:

接口的正式定义:接口就是一组抽象成员的结合.

(1).一个接口定义了一个契约。
(2).接口可以包容方法、C#属性、事件、以及索引器。
(3).在一个接口声明中,我们可以声明零个或者多个成员。
(4).所有接口成员的默认访问类型都是public。接口的命名传统上都以大写I 开头
(5).如果在接口成员声明中包括了任何修饰符,那么会产生一个编译器错误。

常见的接口代码错误例子:

// 内有大量错误!
public interface IPointy
{
// 错误!接口不能有字段!
public int numbOfPoints;
// 错误!接口不能有构造函数!
public IPointy() { numbOfPoints = 0;};
// 错误!接口不能提供实现!
byte GetNumberOfPoints() { return numbOfPoints; }

}

有好多人会问,其实抽象基类和这个不是一样的?写一个类,做集成就好了,干嘛要接口,首先说一下接口和基类的区别

和抽象类相比,接口

1、不能实例化;
2、包含未实现的方法声明;
3、派生类必须实现未实现的方法,抽象类是抽象方法,接口则是所有成员(不仅是方法包括其他成员)

4、接口可以附带一些属性,索引器,以及方法,但必须是公开的

一个类可以直接继承多个接口,但只能直接继承一个类(包括抽象类) ,这就是导致多态这种性质不得已展现

 

另外接口可以作为参数,可以作为返回值,还可以放在数组中

 

 

IEnumerable 和 IEnumerator 接口

 

IEnumerator GetEnumerator() 返回可循环访问集合的枚举数。

IEnumerator:
object Current 获取集合中的当前元素。
bool MoveNext() 将枚举数推进到集合的下一个元素。
如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
void Reset() 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。

 

代码

using System;
using System.Collections;

public class Person
{
    public Person(string fName, string lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public string firstName;
    public string lastName;
}

public class People : IEnumerable
{
    private Person[] _people;
    public People(Person[] pArray)
    {
        _people = new Person[pArray.Length];

        for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }

    public IEnumerator GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
}

public class PeopleEnum : IEnumerator
{
    public Person[] _people;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public PeopleEnum(Person[] list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Length);
    }

    public void Reset()
    {
        position = -1;
    }

    public object Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

class App
{
    static void Main()
    {
        Person[] peopleArray = new Person[3]
        {
            new Person("John", "Smith"),
            new Person("Jim", "Johnson"),
            new Person("Sue", "Rabon"),
        };

        People peopleList = new People(peopleArray);
        foreach (Person p in peopleList)
            Console.WriteLine(p.firstName + " " + p.lastName);

    }
}

/* This code produces output similar to the following:
 * 
 * John Smith
 * Jim Johnson
 * Sue Rabon
 * 
 */

 其实主要还是实现了一个for循环的概念,以前for循环都是计数器功能,现在搞的花样多了,类内循环,对象循环,反正都是一个概念,遍历集合吧

IComparable接口 ,主要是实现集合内的排序

代码

public int CompareTo(object obj)

{

      if( !(obj is Developer) )

            throw new InvalidCastException("Not a valid Developer object.");

      Developer developer = (Developer)obj; 

      return this.ID.CompareTo(developer.ID);  
//使用IComparable为他们排序
//Array.Sort();                          

}
View Code

 

 

 

 

posted @ 2013-08-03 17:26  Vip_reg32  阅读(657)  评论(0)    收藏  举报