将信号列表送入电线

时间:2015-04-04 06:46:03

标签: java

我给了private List<Wire> inputs和方法public void feed(List<Signal> inSigs)。我必须使用方法List<Wire> inputs的参数中的inSigs来更改feed()中的信号(最初每个信号为== Signal.X)。这一切都让我遇到了麻烦。如何通过inSigs更改List输入的状态(注意:参数类型为<Signal>)?我已经完成了smth,但在setSignal(x)下不断得到并强调错误。我附加了两个课程(GateWire下方)

import java.util.*;

    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)
        {
            this.name = name;
            this.output = out;

            if(ins.size() == 0 || ins.isEmpty())
                throw new ExceptionLogicParameters(false, 1, 0);
            else 
                this.inputs = ins;
        }

        @Override
        public void feed(List<Signal> inSigs)
        {

            for(Signal x: inSigs)
                inputs.setSignal(x);
        }

        @Override
        public void feed(String name)
        {
            ((Wire) inputs).setName(name);
        }

    }

    public class Wire {

        private Signal signal;
        private String name;

        public Wire(String name)
        {
            this.name = name;
            this.signal = Signal.X;
        }

        @Override
        public String toString()
        {
            return "\""+ this.name+":"+this.signal+"\"";
        }

        @Override
        public boolean equals(Object other)
        {
            if(other instanceof Wire)
            {
                Wire leftHandside = (Wire)other;
                return this.name.equals(leftHandside.name) && this.signal == leftHandside.signal;
            }
            else 
                return false;
        }

        public Signal getSignal()
        {
            return this.signal;
        }

        public String getString()
        {
            return this.name;
        }

        public void setSignal(Signal signal)
        {
            this.signal = signal;
        }

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

    }

2 个答案:

答案 0 :(得分:1)

您的代码和问题的读取方式存在许多歧义。

我假设信号列表与您的私人电线列表大小相同,然后:

public void feed(List<Signal> inSigs) {
  // Needs precondition that inSigs.size() == input.size()
  for (int i = 0; i < inSigs.size(); i++) {
    inputs.get(i).setSignal(inSigs.get(i));
  }
}

否则你需要一种方法将信号映射到电线,(可能是通过索引)。

答案 1 :(得分:1)

可能你需要这样的东西:

    @Override
    public void feed(List<Signal> inSigs)
    {
        if(inSigs.size() != inputs.size()) {
          throw new ExceptionLogicParameters(false, 1, 0);
        }
        int i = 0;
        for (Signal x: inSigs) {
          inputs.get(i++).setSignal(x);
        }
    }