如何在Java中测试子类是否相等?

问题描述 投票:0回答:2

我正在从事一个项目,但遇到了一些问题。我需要测试子类(DiscountProduct)的相等性。类之间的关系可以理解为:订单类has-a Product,而Product是DiscountProduct的父级。我想测试DiscountProduct是否相等。任何帮助深表感谢。谢谢!下面是代码:

[订单分类:

public class Order implements Comparable<Order>{

    private int quantity;
    //composition has-a relationship
    private Product product;

    public Order(int quantity, Product product) {
        this.quantity = quantity;
        this.product = product;
    }

    public boolean equals(Object obj){
        if (obj instanceof Order) {
            return product.equals(obj) && quantity == ((Order) obj).quantity;
        }
        return false;
    }

    public Product getProduct() {
        return product;
    }
}

产品类别

public class Product implements Comparable<Product> {

    private String productCode;
    private String description;
    private int unitPrice; //pence

    public Product(String productCode, String description, int unitPrice) {
        this.productCode = productCode;
        this.description = description;
        this.unitPrice = unitPrice;
    }

    //equals method to test product for equality.
    public boolean equals(Object obj){
        if (this == obj){
            return true;
        }
        if (obj == null || !(obj instanceof Product)){
            System.out.println("-");

            return false;
        }
        Order other = (Order) obj;
        if (!productCode.equals(other.getProduct().getProductCode()))
            return false;
        if (!description.equals(other.getProduct().getDescription()))
            return false;        
        if (unitPrice != (other.getProduct().getUnitPrice()))
            return false;
        return true;
    }
}

DiscountProduct类

public class DiscountProduct extends Product {

    private double discountRate;

    public DiscountProduct(String productCode, String description, int unitPrice, double discountRate) {
        super(productCode, description, unitPrice);
        this.discountRate = discountRate;
    }

    //equals method to test discount product for equality.
    public boolean equals(Object obj){
        Order other = (Order) obj;
       //how to test for the equality for the discountProduct's field discountRate?
       //does I need to add some method in Order class to get the discountRate of object of Order class, 
       //because relation is 'Order has Product' and then there is a parent-child relation
       //between product and DiscountProduct class. 

    }
}
java equals comparator
2个回答
1
投票

在代码的这一部分:

public class Order implements Comparable<Order>{
    ...
    public boolean equals(Object obj){
        if (obj instanceof Order) {
            return product.equals(obj) ...;
        }
        ...
    }
}

ProductOrder结合。由于false不为Product,因此该比较显然应返回Order。您可能要写的是:

return product.equals(obj.product) ...;

0
投票

DiscountProduct可以执行以下操作:

@Override // always always always use that annotation when overriding!
public boolean equals(Object obj){
  .. this == obj test
  if (obj == null || !(obj instanceof DiscountProduct)){
    return false;
  } 
  if (super.equals(obj)) {
    cast to DiscountProduct and check discountRate

这里的重点是:

  • 您真的only希望相同类的对象相等。否则,您很有可能会错误地得出必要的if a.equals(b) then b.equals(a)规则
  • 然后您想重新使用父类的现有实现
  • 然后最后,比较子类字段
© www.soinside.com 2019 - 2024. All rights reserved.