2015-04-07 60 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