【问题标题】:generic constructor invoking errors?通用构造函数调用错误?
【发布时间】:2017-06-05 23:16:06
【问题描述】:

所以在这段代码中,因为注释表明出现了两种令人困惑的情况。将泛型放在构造函数名称之前和之后会给出不同的结果。

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??


     }}

那么 &lt;Type&gt;constructor()constructor&lt;Type&gt;()有什么区别

【问题讨论】:

标签: java vector constructor


【解决方案1】:

第一个问题的答案:

// 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 上的每个问题创建一个新帖子。

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2014-01-08
    • 2014-10-25
    相关资源
    最近更新 更多