簡體   English   中英

我應該為下面給出的表創建方法還是變量

[英]Should I create a method or variable for the table given below

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.

任何人都可以建議,如果是正確的。我可以這樣使用:
公共類指令隊列{LinkedList queue = new LinkedList()/ *如何從隊列類中添加,刪除和刪除指令消息* /} insturctionmessage是一個類

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個字段,因此您可以創建一個Instruction類來封裝它們。 然后,對於隊列,您可以使用列表,並在列表中填充“說明”。 例如,實例化

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

然后,您可以使用queue.getFirst()和queue.addLast(...)來操縱隊列的內容。

用所有變量創建一個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-如果指定的元素為null,並且此隊列不允許使用null元素
  4. IllegalArgumentException-如果此元素的某些屬性阻止將其添加到此隊列中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM