public class Item {
/**
* Instance variables for this class
*/
private String itemName;
private int itemQuantity;
/**
* Contructor for this class
*/
public Item (String itemName, int itemQuantity) {
this.itemName = itemName;
this.itemQuantity = itemQuantity;
}
//setter and getter methods
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
好吧,我已經有了類的項目。現在我必須編寫CartItem類。是給出的描述如下:從Java中的另一個類獲取數據
class CartItem{
/*
Objects of this class are used to hold items that the shopper purchases in the super market.
There are two attributes in this class, an item (an object created from the Item class) and a quantity (the number of that item that the shopper purchases). You have to write these two attributes. Note that one of the two will have a user defined data type.
*/
}
public class CartItem {
private Item item; //item from the item class
private int itemQuantity; //quantity how much shopper buys
public CartItem(Item itemName, int itemQuantity) {
this.getItem();
this.getQuantity();
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public int getQuantity() {
return itemQuantity;
}
public void setQuantity(int quantity) {
this.itemQuantity = itemQuantity;
}
}
只是想知道,如果它是正確的,但。
是否有隱藏在某處的實際問題?請幫我發現它。 –
如果你問我,高層次的設計似乎是關閉的。爲什麼你在'Item'類和'CartItem'類中都有數量?您應該將數量保留在CartItem類中,並且只關注「Item」類的單個產品的屬性,例如價格,描述等。 – ladaghini
yeahh,我想了解它背後的潛在原因基於講師的設計,它已經被設計出來了,所有的規格都已經給了我們,我們必須嚴格遵守。 – yoshifish