Advertisement
Guest User

Untitled

a guest
May 23rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. class Gen<T> {
  2.   T ob; // declare an object of type T
  3.    
  4.   // Pass the constructor a reference to  
  5.   // an object of type T.
  6.   Gen(T o) {
  7.     ob = o;
  8.   }
  9.  
  10.   // Return ob.
  11.   T getob() {
  12.     return ob;
  13.   }
  14.  
  15.   // Show type of T.
  16.   void showType() {
  17.     System.out.println("Type of T is " +
  18.                        ob.getClass().getName());
  19.   }
  20. }
  21.  
  22. // Demonstrate the generic class.
  23. class GenDemo {
  24.   public static void main(String args[]) {
  25.     // Create a Gen reference for Integers.  
  26.     Gen<Integer> iOb;  
  27.  
  28.     // Create a Gen<Integer> object and assign its
  29.     // reference to iOb.  Notice the use of autoboxing  
  30.     // to encapsulate the value 88 within an Integer object.
  31.     iOb = new Gen<Integer>(88);
  32.  
  33.     // Show the type of data used by iOb.
  34.     iOb.showType();
  35.  
  36.     // Get the value in iOb. Notice that
  37.     // no cast is needed.
  38.     int v = iOb.getob();
  39.     System.out.println("value: " + v);
  40. System.out.println();
  41.  
  42.     // Create a Gen object for Strings.
  43.     Gen<String> strOb = new Gen<String>("Generics Test");
  44.  
  45.     // Show the type of data used by strOb.
  46.     strOb.showType();
  47.  
  48.     // Get the value of strOb. Again, notice
  49.     // that no cast is needed.
  50.     String str = strOb.getob();
  51.     System.out.println("value: " + str);
  52.   }
  53. }
  54. _______________________________
  55.  
  56. // A simple generic class with two type
  57. // parameters: T and V.
  58. class TwoGen<T, V> {
  59.   T ob1;
  60.   V ob2;
  61.    
  62.   // Pass the constructor a reference to  
  63.   // an object of type T.
  64.   TwoGen(T o1, V o2) {
  65.     ob1 = o1;
  66.     ob2 = o2;
  67.   }
  68.  
  69.   // Show types of T and V.
  70.   void showTypes() {
  71.     System.out.println("Type of T is " +
  72.                        ob1.getClass().getName());
  73.  
  74.     System.out.println("Type of V is " +
  75.                        ob2.getClass().getName());
  76.   }
  77.  
  78.   T getob1() {
  79.     return ob1;
  80.   }
  81.  
  82.   V getob2() {
  83.     return ob2;
  84. }
  85. }
  86. / Demonstrate TwoGen.
  87. class SimpGen {
  88.   public static void main(String args[]) {
  89.  
  90.     TwoGen<Integer, String> tgObj =
  91.       new TwoGen<Integer, String>(88, "Generics");
  92.  
  93.     // Show the types.
  94.     tgObj.showTypes();
  95.  
  96.     // Obtain and show values.
  97.     int v = tgObj.getob1();
  98.     System.out.println("value: " + v);
  99.  
  100.     String str = tgObj.getob2();
  101.     System.out.println("value: " + str);
  102.   }
  103. }
  104. _______________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement