2015-04-07 59 views
0

父类中的一个ArrayList:如何创建超(...)

public abstract class Gate implements Logic{ 
    private List<Wire> inputs; 
    private Wire output; 
    private String name; 

    public Gate(String name, List<Wire> ins, Wire out){ 
    } 

子类:

public class GateNot extends Gate{ 
    public GateNot(Wire input, Wire output){ 

    super("Not",new ArrayList(input) ,output);//this is apparently incorrect. 

    } 

参数在GateNot的构造是在父类中的参数不同。我想创建一个数组列表并将输入传递给此数组列表,以便超级(...)可以工作。我如何在super(..)中创建这个数组列表?如果一个数组列表在这里不起作用,我可以用这个超级怎么办?

+0

'新的ArrayList (输入)'? – Constant

+0

可以传递ArrayList而不是List? –

+0

这不起作用.. – user4593157

回答

3

那么,你只有一个输入。所以..

public class GateNot extends Gate { 
    public GateNot(Wire input, Wire output) { 
     super("Not", new ArrayList<Wire>(Arrays.asList(input)), output); 
    } 
} 

编辑:我意识到你有一个清单>的<,而不是一个ArrayList <>所以我们可以简化这个给:

public class GateNot extends Gate { 
    public GateNot(Wire input, Wire output) { 
     super("Not", Arrays.asList(input), output); 
    } 
} 
+0

所以我可以直接在这里使用数组,而不必创建像Wire [] a这样的新数组? – user4593157

+0

Arrays.asList()返回一个List <>。你有*提供一个List <>,因为它在父构造函数中。 – tys