0

我有一个非常简单的问题-

我有一个名为的类DEClient,其构造函数是这样的-

public DEClient(List<DEKey> keys) {
    process(keys);
}

DEKey 上课是这样的——

public class DEKey {

    private String name;
    private String value;

    public DEKey(){
        name = null;
        value = null;
    }

    public DEKey(String name, String value){
        this.name  = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

现在我正在尝试实例化DEClient构造函数。所以为此我需要拥有List<DEKey>.

所以我所做的是我使用(将返回)和作为值来实例化如下所示的DEKey类。service.getKeys()Stringid

DEKey dk = new DEKey(service.getKeys(), id);

//The below line throws exception whenever I am running.
DEClient deClient = new DEClient((List<DEKey>) dk);

我在这里做错了什么?

4

1 回答 1

1

您需要先制作一个List,然后将您的密钥添加到该List。像你所做的那样铸造不是这样做的方法,因为DEKeyis not aList并且铸造它会抛出一个ClassCastException.

DEKey dk = new DEKey(service.getKeys(), id);

List<DEKey> list = new ArrayList<DEKey>();
list.add (dk);

DEClient deClient = new DEClient(list);
于 2013-03-02T22:24:36.087 回答