开发日志 5-31
Better an empty purse than an empty head .
........... * * *...................... * * *....................................
1) 泛型:
最近有点累,好像头脑也有点不太转弯了,泛型这个本来用的挺熟的东西,似乎不会用了。特别是在泛型的继承这块。
(1) 泛型类可以从具体的、封闭式构造或开放式构造基类继承
e.g.
class BaseNode { }
class BaseNodeGeneric<T> { }
// concrete type
class NodeConcrete<T> : BaseNode { }
//closed constructed type
class NodeClosed<T> : BaseNodeGeneric<int> { }
//open constructed type
class NodeOpen<T> : BaseNodeGeneric<T> { }
(2) 非泛型(具体)类可以从封闭式构造基类继承,但无法从开放式构造类或裸类型参数继承,因为在运行时客户端代码无法提供实例化基类所需的类型变量。
e.g.
//No error
class Node1 : BaseNodeGeneric<int> { }
//Generates an error
//class Node2 : BaseNodeGeneric<T> {}
//Generates an error
//class Node3 : T {}
(3) 从开放式构造类型继承的泛型类必须为任何未被继承类共享的基类类型参数提供类型变量,如以下代码所示:
e.g.
class BaseNodeMultiple<T, U> { }
//No error
class Node4<T> : BaseNodeMultiple<T, int> { }
//No error
class Node5<T, U> : BaseNodeMultiple<T, U> { }
//Generates an error
//class Node6<T> : BaseNodeMultiple<T, U> {}
(4) 从开放式构造类型继承的泛型类必须指定约束,这些约束是基类型约束的超集或暗示基类型约束:
e.g.
class NodeItem<T> where T : System.IComparable<T>, new() { }
class SpecialNodeItem<T> : NodeItem<T> where T : System.IComparable<T>, new() { }
(5) 泛型类型可以使用多个类型参数和约束
e.g.
class SuperKeyType<K, V, U>
where U : System.IComparable<U>
where V : new() { }
(6) 泛型约束
where T:class 必须为引用类型 where T:struct 必须为值类型
where T: U
为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。(有点晕)
真理不辨不明,还是写点小程序实践一下,能够有所得。

浙公网安备 33010602011771号