0

我有两个类,第一个类被调用Product,它基本上保存了从数据库加载的所有可用产品及其属性。另一个类叫做OrderProduct, extendsProduct并且还有两个属性:数量和日期。OrderProduct 用作​​保存当前订单信息的临时对象。在订单结束时,订单对象被保存到数据库中。我的问题:我应该遵循我当前的结构还是设计它,OrderProduct 不会扩展 Product 类,只会持有 id 来访问特定产品并具有这两个附加属性:数量和日期?还是有更好的方法来做到这一点?

产品:

package domainLayer;

public class Product {

    private int id;
    private String name;
    private String description;
    private String type;
    private int price;
    private String status;

    public Product(int id, String name, int price, String type, String description, String status) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.price = price;
        this.status = status;
    }

    public Product(Product product){
        this.id = product.getId();
        this.name = product.getName();
        this.description = product.getDescription();
        this.price = product.getPrice();
        this.status = product.getStatus();
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }
}

订购产品:

package domainLayer;

import java.util.Date;

public class OrderProduct extends Product {

    private int quantity;
    private Date date;

    public OrderProduct(int id, String name, int price, String type, String description, String status, int quantity, Date date) {
        super(id, name, price, type, description, status);
        this.quantity = quantity;
        this.date = date;
    }

    public OrderProduct(Product product, int quantity, Date date){
        super(product);
        this.quantity = quantity;
        this.date = date;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public void addQuantity(int quantity){
        this.quantity += quantity;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }
}
4

1 回答 1

0

我不会那样做。OrderProduct不是真正的子类,所以它不应该是 Product 的子类。

而是创建一个带有产品、数量、日期等吸气剂的 OrderProduct。

于 2013-10-17T11:30:14.247 回答