在Scala中使用Java泛型类会抛出错误数量的类型参数错误

时间:2018-05-10 08:48:27

标签: java scala generics arguments

我在Java中有一个泛型类 - RequestWrapper.java

package com.wipro.nepc.coe.requestWrapper;
import java.io.Serializable;

public class RequestWrapper<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    T requestDetails;
    String ownerIpAddress;

    public RequestWrapper(T requestDetails, String ownerIpAddress) {
        this.requestDetails = requestDetails;
        this.ownerIpAddress = ownerIpAddress;
    }

    public T getRequestDetails() {
        return requestDetails;
    }

    public void setRequestDetails(T requestDetails) {
        this.requestDetails = requestDetails;
    }

    public String getOwnerIpAddress() {
        return ownerIpAddress;
    }

    public void setOwnerIpAddress(String ownerIpAddress) {
        this.ownerIpAddress = ownerIpAddress;
    }

}

我能够在另一个Java类中创建RequestWrapper的对象,如下所示:

HashMap<String,Object> testMap = new HashMap<String,Object>();
Object obj = new Integer(1);    
testMap.put("one",obj);

String ownerIp = "10.145.59.107";

RequestWrapper<HashMap<String, Object>> requestWrap = 
        new RequestWrapper<HashMap<String,Object>>(testMap, ownerIp);

我尝试在Scala对象中执行相同的操作:

import java.util.HashMap

var testMap = new HashMap[String,Object]
var obj = new Integer(1);
testMap.put("one", obj);

var ownerIp: String = "10.145.59.107"

var requestWrap = new RequestWrapper[HashMap[String,Object]](testMap,ownerIp)

但是我收到以下错误:

  

错误的类型参数数量   com.wipro.nepc.coe.requestWrapper.RequestWrapper,应为2

为什么Scala中会抛出此错误?泛型类只有一个类型参数,在Java中使用时不会抛出任何错误并且工作正常。但是在scala中,它要求额外的类型参数。

1 个答案:

答案 0 :(得分:0)

看起来错误只是由Eclipse引发的。该项目由Maven成功编译。很奇怪为什么Eclipse抛出了这个错误