如何在java中实现以下C++代码?
Template class <T> a{
     T a=new T();
    
}

解决方案 »

  1.   

    实现不了JAVA中没有C++那样的模板,JAVA的模板只是一个"自动强转"
      

  2.   

    public Test<T> {
      private T a = (T)new Object();
    }
      

  3.   

    不好意思啦~~ public 后面漏了个 class。
      

  4.   

    前两天写的一段代码class StackX<T>
       {
       private int maxSize;
       private Object[] stackArray;
       private int top;
       public StackX(int s)
          {
          maxSize = s;
          stackArray = new Object[maxSize];
          top = -1;
          }
       public void push(T j)
          {
          stackArray[++top] = j;
          }
       public T pop()
          {
          return (T) stackArray[top--];
          }
       public T peek()
          {
          return (T) stackArray[top];
          }
       public boolean isEmpty()
          {
          return (top == -1);
          }
       public boolean isFull()
          {
          return (top == maxSize-1);
          }
       }
    class StackApp
       {
       public static void main(String[] args)
          {
          StackX<Long> theStack = new StackX<Long>(10);
          theStack.push(20L);
          theStack.push(40L);
          theStack.push(60L);
          theStack.push(80L);      while( !theStack.isEmpty() )
             {
             long value = theStack.pop();
             System.out.print(value);
             System.out.print(" ");
             }
          System.out.println("");
          }
       }