0
I have an assignment which says that I need to create an queue class with instruction messages on it.
and instruction message is formed of following properties

 -----------
>InstructionType integer  
> ProductCode    integer  
> Quantity       integer  
> UOM byte

--------------------------------

Can I declare all the above given properties as variables and then pass it as a parameter in a function created for instruction message and place it in the instruction queue class. Can anyone give me a sample code for proceeding with this..and let me know if I think right or any other methodologies. Can be done? please help.

任何人都可以建议,如果是对的。我可以这样使用:
public class instructionqueue { LinkedList queue = new LinkedList() /* 如何从队列类中添加、检索和删除指令消息 */ } 其中指令消息是一个类

public class Instructionmessage 
{

   public int Instructiontype;
   public Integer   Productcode;
   public Integer quantity;
   public byte[] UOM = new byte[256];
   public Integer Timestamp;

  /*method to set and get the instruction type  for the messages*/

   public int getInstructiontype()
   {
       return Instructiontype;
   }
   public void setInstructiontype(int newInstructtype)
   {
       Instructiontype = newInstructtype;
   }

    /*method to set and get the product code   for the messages*/

   public int getProductcode()
   {
       return Productcode;
   }
   public void setProductCode(int newproductcode)
   {
       Productcode = newproductcode;
   }


   /*method to set and get the quantity   for the messages*/
   public int getquantity()
   {
       return quantity;
   }

   public void setquantity(int newquantity)
   {
     quantity = newquantity;
   }

   /*method to set and get the Timestamp   for the messages*/
   public int getTimestamp ()
   {
       return Timestamp;
   }
   public void setTimestamp(int newTimestamp)
   {
       Timestamp = newTimestamp;
   }

   /*method to set and get the UOM   for the messages*/
   public byte[] getUOM()
   {
       return UOM;
   }

   public void setUOM(byte[] newUOM)
   {
       UOM = newUOM;
   }    

}
4

2 回答 2

0

由于指令消息由 4 个字段组成,您可以创建一个指令类来封装它们。然后,对于队列,您可以使用一个列表,并用指令填充它。例如,实例化

LinkedList<Instruction> queue = new LinkedList<Instruction>();

然后,您可以使用 queue.getFirst() 和 queue.addLast(...) 来操作队列的内容。

于 2013-03-29T10:25:38.807 回答
0

使用所有变量创建一个 POJO java 类。为所有变量创建 getter 和 setter。这将帮助您使用这些方法设置和获取数据。

现在在您的主类中创建一个 Queue 对象,并使用 add(Object) 方法将其添加到队列中。

Queue<String> sampleQueue = new LinkedList<String>();
sampleQueue.add("Joseph");
sampleQueue.add("Madhonna");

注意:队列是一个接口,所以不能实例化对象。

使用此方法时可能会引发异常。有关更多详细信息,请参阅此链接http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html#add(E)

  1. IllegalStateException - 如果此时由于容量限制无法添加元素列表项
  2. ClassCastException - 如果指定元素的类阻止它被添加到此队列
  3. NullPointerException - 如果指定元素为空且此队列不允许空元素
  4. IllegalArgumentException - 如果此元素的某些属性阻止它被添加到此队列
于 2013-03-29T21:16:34.203 回答