泛型构造函数调用错误?

时间:2017-01-20 21:25:03

标签: java vector constructor

所以在这段代码中,通知表示会出现两个令人困惑的案例。其中将泛型放在构造函数名之前,然后给出不同的结果。

class moke<T> {
    T ob;
     moke(T ob){
      this.ob=ob;
    }
    T ret(){
        return ob;
      }
    }
    class ramirez{
       public static void main(String args[]){
        moke<Integer> one= new moke<Integer>(9);// it's ok
        moke<String> two=new moke<Integer>("ASDASD"); //  error appears here which is ok
        moke<String> three= new <Integer>moke("knmkm"); //no error here WHY??
        moke<String> four=new <String>moke(9);  //No error hereWHY??


     }}

所以 <Type>constructor()constructor<Type>()

之间有什么区别

1 个答案:

答案 0 :(得分:0)

回答第一个问题:

// here {1,2,3} is an array initializer you may use to define the array
int[] arr = {1,2,3}; 

// this does not work, because this is an assignment expression and not a definition of the array
arr = {1,2,3}; 

为了使这更清楚,只需想象arr是一个在运行时确定(可能不同)大小的数组。这项任务将如何运作?

对于第二个问题,我还没有回答。看起来很奇怪但请为stackoverflow上的每个问题创建一个新帖子。