Question: Which of the following statements is incorrect?
1 class C <U, V> {}
2 class D: C <string, int> {}
3 class E <U, V>: C <U, V> {}
4 class F <U, V >:c <string, int> {}
5 class G: C <U, V> {}
Explanation
1. generic definition
2 D is a common class, C is instantiated generic class IMG src = "http://www.cnblogs.com/blog/smile19.gif">
3 E is a sub-generic type, C is a base generic type, C uses the sub-generic parameter policimg src = "http://www.cnblogs.com/blog/smile19.gif">
4 F is a generic Objective C already has an instance named javasimg src = "http://www.cnblogs.com/blog/smile19.gif">
5G is a class, and C is a generic class. Unzip IMG src = "C: \ Program Files \ BlogJet \ Data \ Smiles \ smilew.gif">
Note
You can include a declaration of the generic type in the base class. However, note: if the base class is a generic class, its type parameters are either instantiated or derived from sub-classes? /FONT>
--------
Generic Type? FONT>
Class C <V> {
Public V f1; // declare f1
Public D <V> f2; // reference other generic types to define Member f2
Public C (V x) {// Method
This. f1 = x;
}
}
---------
Generic Interface
Define a method to import a generic type. Define BR> interface IList <T> {
T {} MethodA ();
}
Interface IDictionary <K, V> {
Void MethodB (K k, V v );
}
Class List <T>: IList <T>, IDictionary <int, T>
{
Public T [] MethodA (){}
Public void MethodB (int I, T t ){}
}
----------
Generic Method
Public class Finder {
Public static int Find <T> (T [] items, T item)
{
For (int I; I <items. Length; I ++ ){
If (items [I]. Equals (item) {return I ;}
}
}
}
----------
What is the weight of the generic method? FONT>
Class c {
Void F <T> (int x );
Void F (int x );
Public abstract t f <T, U> (T t, U u) where U: T;
}
Rewrite
Class cc: c {
Public override x f <X, Y> (X x X, Y y ){}
}
-----------------
Base Constraint
Class A {public void F1 (){...}}
Class B {public void F2 (){...}}
Class C <S, T>
Where S:
Where T: B
{
// You can call the F1 Method on type S.
// The F2 method can be called on type B
...
}