1.未使用泛型的Stack类
1
using
System;
2
3 public class Stack
4 {
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 object[] m_Items;
8 public Stack(): this(100)
9 { }
10 public Stack(int size)
11 {
12 m_Size = size;
13 m_Items = new object[m_Size];
14 }
15 public void Push(object item)
16 {
17 if (m_StackPointer >= m_Size)
18 throw new *Exception();
19
20 m_Items[m_StackPointer] = item;
21 m_StackPointer++;
22 }
23 public object Pop()
24 {
25 m_StackPointer--;
26 if (m_StackPointer >= 0)
27 {
28 return m_Items[m_StackPointer];
29 }
30 else
31 {
32 m_StackPointer = 0;
33 throw new InvalidOperationException("Cannot pop an empty stack");
34 }
35 }
36}
37
2
3 public class Stack
4 {
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 object[] m_Items;
8 public Stack(): this(100)
9 { }
10 public Stack(int size)
11 {
12 m_Size = size;
13 m_Items = new object[m_Size];
14 }
15 public void Push(object item)
16 {
17 if (m_StackPointer >= m_Size)
18 throw new *Exception();
19
20 m_Items[m_StackPointer] = item;
21 m_StackPointer++;
22 }
23 public object Pop()
24 {
25 m_StackPointer--;
26 if (m_StackPointer >= 0)
27 {
28 return m_Items[m_StackPointer];
29 }
30 else
31 {
32 m_StackPointer = 0;
33 throw new InvalidOperationException("Cannot pop an empty stack");
34 }
35 }
36}
37
2.使用泛型的类
1
using
System;
2
3 public class Stack < T >
4 {
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 T[] m_Items;
8 public Stack()
9 : this(100)
10 {
11 }
12 public Stack(int size)
13 {
14 m_Size = size;
15 m_Items = new T[m_Size];
16 }
17 public void Push(T item)
18 {
19 if (m_StackPointer >= m_Size)
20 throw new *Exception();
21
22 m_Items[m_StackPointer] = item;
23 m_StackPointer++;
24 }
25 public T Pop()
26 {
27 m_StackPointer--;
28 if (m_StackPointer >= 0)
29 {
30 return m_Items[m_StackPointer];
31 }
32 else
33 {
34 m_StackPointer = 0;
35 //throw new InvalidOperationException("Cannot pop an empty stack");
36 return default(T);
37 }
38 }
39}
40
41 public class Stack1 < T > : Stack < T >
42 {
43
44}
45
2
3 public class Stack < T >
4 {
5 readonly int m_Size;
6 int m_StackPointer = 0;
7 T[] m_Items;
8 public Stack()
9 : this(100)
10 {
11 }
12 public Stack(int size)
13 {
14 m_Size = size;
15 m_Items = new T[m_Size];
16 }
17 public void Push(T item)
18 {
19 if (m_StackPointer >= m_Size)
20 throw new *Exception();
21
22 m_Items[m_StackPointer] = item;
23 m_StackPointer++;
24 }
25 public T Pop()
26 {
27 m_StackPointer--;
28 if (m_StackPointer >= 0)
29 {
30 return m_Items[m_StackPointer];
31 }
32 else
33 {
34 m_StackPointer = 0;
35 //throw new InvalidOperationException("Cannot pop an empty stack");
36 return default(T);
37 }
38 }
39}
40
41 public class Stack1 < T > : Stack < T >
42 {
43
44}
45
下为PDF文档,我感觉挺好的,很简单,我听的懂就是好的
/Clingingboy/one.pdf
多个泛型
1
class
Node
<
K, T
>
2 {
3 public K Key;
4 public T Item;
5 public Node<K, T> NextNode;
6 public Node()
7 {
8 Key = default(K);
9 Item = default(T);
10 NextNode = null;
11 }
12 public Node(K key, T item, Node<K, T> nextNode)
13 {
14 Key = key;
15 Item = item;
16 NextNode = nextNode;
17 }
18}
2 {
3 public K Key;
4 public T Item;
5 public Node<K, T> NextNode;
6 public Node()
7 {
8 Key = default(K);
9 Item = default(T);
10 NextNode = null;
11 }
12 public Node(K key, T item, Node<K, T> nextNode)
13 {
14 Key = key;
15 Item = item;
16 NextNode = nextNode;
17 }
18}
泛型别名
1
using
list
=
LinkedList
<
int
,
string
>
;
泛型约束
1
public
class
LinkedList
<
K, T
>
where K : IComparable
2 {
3 Node<K, T> m_Head;
4 public LinkedList()
5 {
6 m_Head = new Node<K, T>();
7 }
8 public void AddHead(K key, T item)
9 {
10 Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
11 m_Head.NextNode = newNode;
12 }
13
14 T Find(K key)
15 {
16 Node<K, T> current = m_Head;
17 while (current.NextNode != null)
18 {
19 if (current.Key.CompareTo(key) == 0)
20 break;
21 else
22 current = current.NextNode;
23 }
24 return current.Item;
25 }
26
27}
28
2 {
3 Node<K, T> m_Head;
4 public LinkedList()
5 {
6 m_Head = new Node<K, T>();
7 }
8 public void AddHead(K key, T item)
9 {
10 Node<K, T> newNode = new Node<K, T>(key, item, m_Head.NextNode);
11 m_Head.NextNode = newNode;
12 }
13
14 T Find(K key)
15 {
16 Node<K, T> current = m_Head;
17 while (current.NextNode != null)
18 {
19 if (current.Key.CompareTo(key) == 0)
20 break;
21 else
22 current = current.NextNode;
23 }
24 return current.Item;
25 }
26
27}
28
1
using
System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace VS2005Demo1
6 {
7 public class MyBaseClassGeneric // sealed,static
8 {
9 }
10
11 interface IMyBaseInterface
12 {
13 void A();
14 }
15
16 internal class GenericClass<T> where T : MyBaseClassGeneric,IMyBaseInterface
17 {
18
19 }
20
21 class GClass<K, T> where K : MyBaseClassGeneric,IMyBaseInterface,new() where T : K
22 {
23
24 }
25
26 class GUClass<K, T> where T : K where K : MyBaseClassGeneric,IMyBaseInterface, new()
27 {
28 GClass<K, T> obj = new GClass<K, T>();
29 }
30
31
32 不能将引用/值类型约束与基类约束一起使用,因为基类约束涉及到类
38
39 不能使用结构和默认构造函数约束,因为默认构造函数约束也涉及到类
45
46 虽然您可以使用类和默认构造函数约束,但这样做没有任何价值
64
65 可以将引用/值类型约束与接口约束组合起来,前提是引用/值类型约束出现在约束列表的开头
74}
75
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace VS2005Demo1
6 {
7 public class MyBaseClassGeneric // sealed,static
8 {
9 }
10
11 interface IMyBaseInterface
12 {
13 void A();
14 }
15
16 internal class GenericClass<T> where T : MyBaseClassGeneric,IMyBaseInterface
17 {
18
19 }
20
21 class GClass<K, T> where K : MyBaseClassGeneric,IMyBaseInterface,new() where T : K
22 {
23
24 }
25
26 class GUClass<K, T> where T : K where K : MyBaseClassGeneric,IMyBaseInterface, new()
27 {
28 GClass<K, T> obj = new GClass<K, T>();
29 }
30
31
32 不能将引用/值类型约束与基类约束一起使用,因为基类约束涉及到类
38
39 不能使用结构和默认构造函数约束,因为默认构造函数约束也涉及到类
45
46 虽然您可以使用类和默认构造函数约束,但这样做没有任何价值
64
65 可以将引用/值类型约束与接口约束组合起来,前提是引用/值类型约束出现在约束列表的开头
74}
75
记录一下示例代码