2016-05-21 25 views
1

私のコードについて問題がありますが、見つけられないようです。私の問題は、配列に保存する値がオブジェクトに保存されないということです。バイトに値が割り当てられていません

private byte id; 
/** 
* Byte containing the amount of cups of coffee. Amount is represented as a 
* number (Integer) 
*/ 
private byte coffee; 
/** 
* Byte indicating the amount of cafe Latté that has been ordered. Amount 
* is represented as a number (Integer) 
*/ 
private byte cafeLatte; 
/** 
* Byte indicating the amount of smoothies that has been ordered. Amount 
* is represented as a number (Integer) 
* 
*/ 
private byte smoothie; 
/** 
* Byte indicating the amount of Ice coffee that has been ordered. Amount 
* is represented as a number (Integer) 
*/ 
private byte iceCoffee; 
/** 
* Boolean showing if hot drinks has been served or not. 
*/ 
private boolean hotServed; 
/** 
* Boolean showing if cold drinks has been served or not. 
*/ 
private boolean coldServed; 

public Order(byte[] orders) { 
    id = orders[0]; 
    coffee = orders[1]; 
    cafeLatte = orders[2]; 
    smoothie = orders[3]; 
    iceCoffee = orders[4]; 
} 

public byte getId() { 
    return id; 
} 

public boolean coldServed() { 
    return coldServed; 
} 

public void serveCold(boolean coldServed) { 
    this.coldServed = coldServed; 
} 

public void serveHot(boolean served) { 
    hotServed = served; 
} 

public boolean hotServed() { 
    return hotServed; 
} 

/** 
* Method for converting the order into a byte array. 
* @return a 5 columns byte array containing id, coffee, cafe latté, 
* smooties, ice coffees. 
*/ 
public byte[] toByteArray() { 
    return new byte[] {id, coffee, cafeLatte, smoothie, iceCoffee}; 
} 

/** 
* Method for retrieving all the ordered cold drinks with id first. 
* @return An array with numbers indicating the amount of smoothies and 
* Ice coffee to make. Id of the order is first, smoothies next and last 
* Ice coffee. 
*/ 
public byte[] getColdDrinks() { 
    return new byte[] {id, smoothie, iceCoffee}; 
} 

/** 
* Method for retrieving all the ordered hot drinks with id first. 
* @return An array with numbers indicating the amount of coffee and 
* cafelatte to brew. Id of the order is first, coffee next and last 
* cafeLatte. 
*/ 
public byte[] getHotDrinks() { 
    return new byte[] {id, coffee, cafeLatte}; 
} 

私はデバッグを実行すると私のOrderクラスは、単純な

private LinkedList<Order> orders = new LinkedList<Order>(); 

      @Override 
      public void handleDelivery(String tag, Envelope env, AMQP.BasicProperties props, byte[] body) 
        throws IOException { 
       byte[] tmp = new byte[5]; 
       tmp[0] = nextID; 
       nextID++; 
       channel.basicPublish("", "toClient", null, new byte[]{tmp[0]}); 
       for (int i = 1; i < tmp.length; i++) 
        tmp[i] = body[i-1]; 
       orders.add(new Order(tmp)); 
       processOrders(tmp); 

です

:コードは(クラスのトップである@Override部分はrun()メソッド、およびリンクリストの定義であることに注意してくださいされますEclipseでは、handleDeliveryメソッドのtmp配列がボディ配列から正しい値を取得し、 orders.add(new Order(tmp));が値を id, 0,0,0,0(rabbitmqから受け取ったIDが正しいIDであることに注意してください)だが、私がlinkedListに要素を追加していることがわかります。コーヒーを注文すると、何とか orders.add(tmp));と電話すると値1が失われます。

答えて

0

オブジェクトはクラスによってインスタンス化されるべきではありません(定数を除く)が、コンストラクタまたは実行メソッドによってインスタンス化されるべきではありません。クラス内のprivate LinkedList<Order> orders = new LinkedList<Order>();からprivate LinkedList<Order> orders;に変更し、run()orders = new LinkedList<Order>();を追加すると問題が解決しました。

+0

"オブジェクトはクラスによってインスタンス化されるのではなく、コンストラクタまたは実行メソッドによってインスタンス化されるべきです。"これはどこから得たのですか?クラスレベルでオブジェクトをインスタンス化するのはまったく問題ありません。初期化を 'run'メソッドに移して問題を解決するのはなぜですか? – Turing85

+0

いいえ、私は理由を説明することはできませんが、私はその変更を行うことで問題を解決したときに、違いはあると思っています。 – Zuenonentu

関連する問題