Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1.  
  2. import java.lang.reflect.ParameterizedType;
  3. import java.lang.reflect.Type;
  4.  
  5.  
  6.  
  7. final class Method<T> {
  8.  
  9.     private Class<T> typeArgumentClass;
  10.     public Method(Class<T> typeArgumentClass) {
  11.         this.typeArgumentClass = typeArgumentClass;
  12.     }
  13.     public T instance() throws Exception {
  14.         return typeArgumentClass.newInstance();
  15.     }
  16. }
  17. /* Name of the class has to be "Main" only if the class is public. */
  18. class A<T>{
  19.     public Method<T> temp;
  20.     public A(T typeArgumentClass) {
  21.         temp = new Method(typeArgumentClass.getClass());
  22.     }
  23.     public String toString(){
  24.         try {
  25.             return "A<"+temp.instance().toString()+">";
  26.         } catch (Exception e) {
  27.             // TODO Auto-generated catch block
  28.             e.printStackTrace();
  29.         }
  30.         return null;   
  31.     }  
  32. }
  33. class B<T>{
  34.     public Method<T> temp;
  35.     public B(T typeArgumentClass) {
  36.         temp = new Method(typeArgumentClass.getClass());
  37.     }
  38.     public String toString(){
  39.         try {
  40.             return "B<"+temp.instance().toString()+">";
  41.         } catch (Exception e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.         return null;   
  46.     }  
  47. }
  48. class C{
  49.     public String toString(){
  50.         return "C";        
  51.     }
  52. }
  53.  
  54. class test
  55. {
  56.     public static void main (String[] args) throws java.lang.Exception
  57.     {
  58.         B<C> b = new B<C>(new C());
  59.         //System.out.println(b.toString());
  60.         //A<B> a = new A<B>(b);
  61.         A<B<C>> a = new A<B<C>>(b);
  62.         System.out.println(a.toString());
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement