坚持 成长 每日一篇
泛型的好处:提高代码的安全,和减少类型的判断。OC似乎没有范型,所以swift就诞生了!
泛型集合
1.除了没有装箱、拆箱,而且也不可能将错误的类型保存到集合中。
using System;
namespace C的范型
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
//无类型安全检测的的线性列表
LinkedList list = new LinkedList();
for(int i = 0;i<5;i++){
list.add (i);
}
Console.WriteLine ("Doing...");
Console.WriteLine(list);
Console.WriteLine ("Adding employee...");
list.add ( new Employee("John"));
list.add ( new Employee("lucy"));
list.add ( new Employee("Mac"));
list.add ( new Employee("Jay"));
list.add ( new Employee("Marin"));
Console.WriteLine(list);
Console.WriteLine("Done");
//由于此线性列表是无类型检测的所以我们要进行一次强转才能使用
for (int i = 5; i < 10; i++) {
Employee employee = (Employee)list[i];
Console.WriteLine(employee.ToString ());
}
//有类型检测的带泛型的线性列表
NodeList<Employee> node = new NodeList<Employee>();
Console.WriteLine ("Adding expecial employee...");
node.add ( new Employee("John"));
node.add ( new Employee("lucy"));
node.add ( new Employee("Mac"));
node.add ( new Employee("Jay"));
node.add ( new Employee("Marin"));
/*
* node.add ("aaa");此句传入的是string类型对象,由于node被限制为Employee类型所以这句话编译不通过
*/
Console.WriteLine(node);
Console.WriteLine("Done");
//此处我们使用表里面的数据时候就不需要强转
for (int i = 0; i < 5; i++) {
Employee employee = node [i];
Console.WriteLine(employee.ToString ());
}
Employee employee1 = node [5];
if(employee1 == null)
{
Console.WriteLine("Return default(T)就是以为返回null的意思");
}
}
}
//泛型的出现是为了增加类型的安全的检测,减少装箱和拆箱的步骤,减少错误的发生,同时,减少程序开发的代码量,实现不同实例的代码共享,由于泛型是运行时才被拓展成特殊类型
/*为了创建我们的简单无类型限制的线性链表,我们需要下面三个类:
1、Node 类,包含数据以及下一个Node的引用。
2、LinkedList 类,包含链表中的第一个Node,以及关于链表的任何附加信息。
3、测试程序,用于测试 LinkedList 类。
*/
//创建一个Employee类
class Employee
{
private string name;
public Employee(string name)
{
this.name = name;
}
public override string ToString ()
{
return this.name;
}
public void Speack(){
// Console.WriteLine("我是{0}",this.ToString);
}
}
class Note
{
private Object data;
private Note next;
public Object Data{
get { return this.data; }
set { data = value; }
}
public Note Next{
get { return this.next; }
set { this.next = value; }
}
public Note(Object data)
{
this.data = data;
this.next = null;
}
public override string ToString ()
{
string outPut = data.ToString();
if (this.next != null) {
outPut +=", " + this.next.ToString();
}
return outPut;
}
public void Append(Object data)
{
if (this.next == null) {
this.next = new Note (data);
} else {
this.next.Append (data);
}
}
}
class LinkedList
{
Note headNote = null;
public void add(object data)
{
if (headNote == null) {
headNote = new Note (data);
} else {
headNote.Append (data);
}
}
public override string ToString ()
{
return headNote.ToString();
}
//为了提供一点集合的感觉,我们为线性链表创建一个索引器。
public object this[ int index ]{
get{
int ctr = 0;
Note node = headNote;
while ( node != null && ctr <= index ){
if ( ctr == index ){
return node.Data;
}else{
node = node.Next;
}
ctr++;
}
return null;
}
}
}
//上面的LinkedList是一个非类型安全的线性表,也就是说他对于存如的数据类型没有限制,都是以Object类型保持,我们要使用里面的类型是很好,都要进行封箱和拆箱过程。如果我们要做出类型安全的我们只需把Note类下的
//private Note next;的Note类型换成我们需要保存的类型,一次类推修改Object为所需要的类型就会实现对输入类型的限制,但是这样每有一个类型我们就需要添加相同的代码,此时C#的泛型就会为我们解决同类代码的输入
//同时又能实现功能,同时对于该
/*通过泛型定义出安全的线性表结构,其结构如下*/
class Node<T>{
T data;
Node<T> next;
public T Data{
get{
return data;
}
set{
data = value;
}
}
public Node<T> Next{
get{
return next;
}
set{
next = value;
}
}
public Node(T data)
{
this.data = data;
this.next = null;
}
public override string ToString ()
{
string outPut = this.data.ToString();
if (this.next != null) {
outPut += "," + this.next.ToString ();
}
return outPut;
}
public void append(T data)
{
if (this.next == null) {
this.next = new Node<T> (data);
} else {
this.next.append (data);
}
}
}
class NodeList<T>{
Node<T> headNode;
public void add(T data)
{
if (headNode == null) {
headNode = new Node<T> (data);
} else {
headNode.append (data);
}
}
public override string ToString ()
{
return headNode.ToString();
}
//为了提供一点集合的感觉,我们为线性链表创建一个索引器。
public T this[ int index ]{
get{
int ctr = 0;
Node<T> node = headNode;
while ( node != null && ctr <= index ){
if ( ctr == index ){
return node.Data;
}else{
node = node.Next;
}
ctr++;
}
return default(T);//此处返回泛的null必须为default(T)
}
}
}
}