博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# -- 泛型
阅读量:4116 次
发布时间:2019-05-25

本文共 4531 字,大约阅读时间需要 15 分钟。

泛型的概念--“通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用”

BubbleSort定义成泛型类 定义泛型类的一种方法是在类后面加上“<T>” 

//定义泛型类SortHelper 这里“where T:IComparable” 是给类型参数T一个限制 -- 参数类型必须实现IComparable接口,否则无法通过编译    public class SortHelper
where T:IComparable { public void BubbleSort(T[] arr) { int length = arr.Length; for (int i = 0; i < length-1; i++) { for (int j = 0; j < length-1-i; j++) { if (arr[j].CompareTo(arr[j+1])>0) { T temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } }

public class cat:IComparable    {        public string name;        public int price;        public int CompareTo(object obj)        {            cat catT = (cat)obj;            return this.price.CompareTo(catT.price);        }        public cat(string name, int price)        {            this.price = price;            this.name = name;        }    }

static void Main(string[] args)        {            SortHelper
sorter = new SortHelper
(); byte[] a = { 4,5,1,3,2,8,5,0,2}; sorter.BubbleSort(a); SortHelper
sorter1 = new SortHelper
(); int[] b = { 4, 5, 1, 3, 2, 8, 5, 0, 2 }; sorter1.BubbleSort(b);
 
SortHelper
sorter2 = new SortHelper
(); cat cat1=new cat("猫1",1000); cat cat2=new cat("猫2",1400); cat cat3=new cat("猫3",400); cat[] c = { cat1, cat2, cat3 }; sorter2.BubbleSort(c);        //输出省略 }
 
IEnumerable
接口的IEnumerator
GetEnumerator()方法返回了一个迭代器 ,不难发现T如果用 out 标记,则T代表了输出,也就说只能作为结果返回。 IComparable
接口的CompareTo(T other)方法传入了一个T类型的Other参数,不难发现T如果用 in 标记,则T代表了输入,也就是它只能作为参数传入。为了在foreach中使用 People的实例, 我们给People实现IEnumerable接口,代码如下:
 
public class People:IEnumerable    {        private Person[] _people;        public People(Person[] pArray)        {            //实例化数组 用于存Person实例            _people = new Person[pArray.Length];            for (int i = 0; i < pArray.Length; i++)            {                _people[i] = pArray[i];            }        }////IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,可以通过IEnumerable的GetEnumerator()得到IEnumerator对象。        IEnumerator IEnumerable.GetEnumerator()        {            return (IEnumerator)GetEnumerator();        }        public PeopleEnum GetEnumerator()        {            return new PeopleEnum(_people);        }    }    public class PeopleEnum:IEnumerator    {        public Person[] _people;        public PeopleEnum(Person [] pArray)        {            _people = pArray;        }        //游标        int position = -1;        //是否可以往下 移        public bool MoveNext()        {            position++;            return (position < _people.Length);        }        //集合的所有元素取完了之后 重置position        public void Reset()        {            position = -1;        }        //实现 IEnumerator的 Current方法 返回当前所指的Person对象        object IEnumerator.Current        {            get            {                return Current;            }        }        //Current是返回Person类实例的只读方法        public Person Current        {            get            {                try                {                    return _people[position];                }                catch (IndexOutOfRangeException)                {                    throw new InvalidOperationException();                }            }        }    }
static void Main(string[] args)        {            Person[] personArray = new Person[3]{            new Person("Keiling1"),            new Person("Keiling2"),            new Person("Keiling3"),            };            People people = new People(personArray);            foreach (Person item in people)            {                Console.WriteLine(item.name);            }        }
1.一个集合要支持foreach方式的遍历,必须实现IEnumerable接口,描述这类实现了该接口的对象,我们叫它 ‘序列’。 比如 List
支持 foreach 遍历 是因为它实现了IEnumerable接口和其泛型版,2. IEnumerator对象具体实现了迭代器(通过MoveNext(),Reset(),Current)。
3. 从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现迭代器, 而IEnumerator是一个实现式的接口,IEnumerator对象就是一个迭代器。   
4.由于IEnumerable
继承了IEnumerable接口,所以要实现IEnumerator
,还需要实现IEnumerator接口,由于和泛型版本的方法同名,所以该方法的实现需要使用显式接口实现。这里就不继续介绍它的具体实现了,和IEnumerator基本一致,这里就不详述了,读者可以自己动手写一下。
 
 
 
 

转载地址:http://tjupi.baihongyu.com/

你可能感兴趣的文章
9个web前端程序员需要知道的有用Web应用程序
查看>>
在Angular7中如何创建拖放列表?
查看>>
【开发小技巧】32—如何利用HTML、CSS和jQueryUI创建拖放功能以对图像进行重新排序?...
查看>>
血淋淋的事实告诉你:你为什么不应该在JS文件中保存敏感信息
查看>>
设计模式之代理模式
查看>>
Vite2 项目工程化和原理,一次讲清楚
查看>>
21个可以提升你的网站设计水平的网站动画工具
查看>>
设计模式之观察者模式
查看>>
20个关于程序员的笑话,看懂了,你就不会笑了,也不会羡慕他们工资高了!...
查看>>
你应该知道的3个强大的CSS功能
查看>>
12个Web开发工程师会犯的错误,有你吗?
查看>>
华为一夜裁掉7000员工:别在最好的年纪,活得太安逸!
查看>>
5个你可能不知道的很棒CSS功能
查看>>
6种你应该知道的强大JavaScript对象方法
查看>>
在职场,这10句话千万不能在公司说,任何一句,就足以毁掉你的职场生涯!...
查看>>
11个你不应该错过的JavaScript库
查看>>
7个你可能不知道但有用的HTML属性
查看>>
JavaScript 执行环境与作用域、函数的创建和调用
查看>>
董明珠:没有人才,一切归零;没有道德,人才归零
查看>>
设计模式之策略模式
查看>>