0
我正在編寫我的新Java項目,並且要求是表示可以屬於某個類別的產品。 我在我的項目中使用數據庫,並通過外鍵連接產品和類別。 在代碼中,我必須使用SOLID設計,但我不明白如何連接產品和類別。 在第一個版本,代碼爲將產品連接到類別
public class Product {
private int ID;
private String name;
private String descr;
private int stock;
private float price;
private int category;
public Product(int anID, String aName, String aDescr, int aStock, float aPrice, int aCategory) {
this.ID = anID;
this.name = aName;
this.descr = aDescr;
this.stock = aStock;
this.price = aPrice;
this.category = aCategory;
}
public int getID() { return this.ID; }
public String getName() { return this.name; }
public String getDescr() { return this.descr; }
public int getStock() { return this.stock; }
public float getPrice() { return this.price; }
public int getCategory() { return this.category; }
public void decreaseStock(int x) { this.stock -= x; }
}
和
public class Category {
private int ID;
private String name;
private String descr;
public Category (int anID, String aName, String aDescr) {
this.ID = anID;
this.name = aName;
this.descr = aDescr;
}
public int getID() { return this.ID; }
public String getName() { return this.name; }
public String getDescr() { return this.descr; }
}
...但我認爲產品可以工具類別,以便在一個所有信息對象,而不是在兩個類之間跳轉...
哪一個是寫它的最好方法?