上一篇 | 下一篇

c#泛型学习(二)

发布: 2008-6-30 09:24 | 作者: admin | 来源: | 查看: 0次

1.泛型和泛型强制转换

1using System;

2using System.Collections.Generic;

3using System.Text;

4

5namespace VS2005Demo2

6{

7

8 C# 编译器只允许将泛型参数隐式强制转换到 Object 或约束指定的类型#region C# 编译器只允许将泛型参数隐式强制转换到 Object 或约束指定的类型

9 public interface ISomeInterface

10 { }

11 class BaseClass

12 { }

13 class MyClass where T : BaseClass, ISomeInterface

14 {

15 void SomeMethod(T t)

16 {

17 ISomeInterface obj1 = t;

18 BaseClass obj2 = t;

19 object obj3 = t;

20 }

21 }

22 #endregion

23

24 编译器允许您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类#region 编译器允许您将泛型参数显式强制转换到其他任何接口,但不能将其转换到类

25 class SomeClass

26 { }

27 //class MyClass1

28 //{

29 // void SomeMethod(T t)

30 // {

31 // ISomeInterface obj1 = (ISomeInterface)t; //Compiles

32 // SomeClass obj2 = (SomeClass)t; //Does not compile

33 // }

34 //}

35 #endregion

36

37

38 使用临时的 Object 变量,将泛型参数强制转换到其他任何类型#region 使用临时的 Object 变量,将泛型参数强制转换到其他任何类型

39 class MyClass2

40 {

41 void SomeMethod(T t)

42 {

43 object temp = t;

44 SomeClass obj = (SomeClass)temp;

45 }

46 }

47 #endregion

48

49 使用is和as运算符#region 使用is和as运算符

50 public class MyClass3

51 {

52 public void SomeMethod(T t)

53 {

54 if (t is int) { }

55 if (t is LinkedList) { }

56 string str = t as string;

57 if (str != null) { }

58 LinkedList list = t as LinkedList;

59 if (list != null) { }

60 }

61 }

62 #endregion

63

64}

65

2.继承和泛型

1using System;

2using System.Collections.Generic;

3using System.Text;

4

5namespace VS2005Demo2

6{

7 继承和泛型#region 继承和泛型

8 public class BaseClass

9 { }

10 public class SubClass : BaseClass

11 { }

12

13

14 public class SubClass1 : BaseClass

15 { }

16 #endregion

17

18 继承约束#region 继承约束

19 public class BaseClass1 where T : ISomeInterface

20 { }

21 public class SubClass2 : BaseClass1 where T : ISomeInterface

22 { }

23

24 //构造函数约束

25 public class BaseClass3 where T : new()

26 {

27 public T SomeMethod()

28 {

29 return new T();

30 }

31 }

32 public class SubClass3 : BaseClass3 where T : new()

33 { }

34

35 #endregion

36

37 虚拟方法#region 虚拟方法

38 public class BaseClass4

39 {

40 public virtual T SomeMethod()

41 {

42 return default(T);

43 }

44 }

45 public class SubClass4 : BaseClass4

46 {

47 public override int SomeMethod()

48 {

49 return 0;

50 }

51 }

52

53 public class SubClass5 : BaseClass4

54 {

55 public override T SomeMethod()

56 {

57 return default(T);

58 }

59 }

60

61 #endregion

62

63 接口、抽象类继承#region 接口、抽象类继承

64 public interface ISomeInterface6

65 {

66 T SomeMethod(T t);

67 }

68 public abstract class BaseClass6

69 {

70 public abstract T SomeMethod(T t);

71 }

72 public class SubClass6 : BaseClass6,ISomeInterface6

73 {

74 public override T SomeMethod(T t)

75 { return default(T); }

76 }

77 #endregion

78

79 泛型抽象方法和泛型接口#region 泛型抽象方法和泛型接口

80 //public class Calculator

81 //{

82 // public T Add(T arg1, T arg2)

83 // {

84 // return arg1 + arg2;//Does not compile

85 // }

86 // //Rest of the methods

87 //}

88

89 public abstract class BaseCalculator

90 {

91 public abstract T Add(T arg1, T arg2);

92 //public abstract T Subtract(T arg1, T arg2);

93 //public abstract T Divide(T arg1, T arg2);

94 //public abstract T Multiply(T arg1, T arg2);

95 }

96 public class MyCalculator : BaseCalculator

97 {

98 public override int Add(int arg1, int arg2)

99 {

100 return arg1 + arg2;

101 }

102 //Rest of the methods

103 }

104

105 public interface ICalculator

106 {

107 T Add(T arg1, T arg2);

108 //Rest of the methods

109 }

110 public class MyCalculator1 : ICalculator

111 {

112 public int Add(int arg1, int arg2)

113 {

114 return arg1 + arg2;

115 }

116 //Rest of the methods

117 }

118 #endregion

119

120}

121

字号: | 推荐给好友

51/512345>

评分:0

我来说两句