A simple generic class with two type parameters: T and V. : Generic Parameter « Generics « Java






A simple generic class with two type parameters: T and V.

A simple generic class with two type parameters: T and V.
 
class TwoGen<T, V> {
  T ob1;

  V ob2;

  TwoGen(T o1, V o2) {
    ob1 = o1;
    ob2 = o2;
  }

  void showTypes() {
    System.out.println("Type of T is " + ob1.getClass().getName());
    System.out.println("Type of V is " + ob2.getClass().getName());
  }

  T getob1() {
    return ob1;
  }

  V getob2() {
    return ob2;
  }
}

public class SimpGen {
  public static void main(String args[]) {
    TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics");
    tgObj.showTypes();
    int v = tgObj.getob1();
    System.out.println("value: " + v);
    String str = tgObj.getob2();
    System.out.println("value: " + str);
  }
}


           
         
  








Related examples in the same category

1.Java generic: Hierarchy argumentJava generic: Hierarchy argument
2.Boxing Generic Example
3.Demonstrate a raw generic type. Demonstrate a raw generic type.
4.T is a type parameter that will be replaced by a real type when an object of type Gen is created.
5.Create a generic class that can compute the average of an array of numbers of any given type.
6.the type argument for T must be either Number, or a class derived from Number.
7.Demonstrate a raw type.
8.A subclass can add its own type parameters.
9.Compare two generic parameters
10.Default implementation of {@link java.lang.reflect.ParameterizedType}
11.Get the Generic definition from a class for given class with given index.