将对象中的值与参数的值进行比较

时间:2016-03-06 19:58:07

标签: java

我正在为我所在的实验室上课,我需要实现此功能:

// 6. ***** Write this method *****
// method name:  equals
// return value: boolean
// parameter:    Airport object
// function:     returns true if airportCode
//                and gates in this object
//                are equal to those in the parameter object;
//               returns false otherwise

老实说,这不是一个年级,而是我的理解。我根本不知道如何将当前对象与参数进行比较。这是什么解决方案?

2 个答案:

答案 0 :(得分:0)

简而言之,您应该覆盖equals类的Object方法。

由于没有关于门是否保存为数组,List / SetCollection)或完全不同class的对象的信息,提供了所有三种解决方案。

@Override
public boolean equals(Object other) {
    if(other == null)
        return false;               // Nothing is equal to null, other than null (in which case 
                                    // this method would not be callable)
    if(this == other)
        return true;                // They are already the same reference
    if(!(other instanceof Airport))
        return false;               // An Airport can not be considered equal to a non-Airport object

    Airport port = (Airport) other; // This cast is now safe, as other must 
                                    // be of type Airport

    // Until next END, this can be removed if airportCode can never be null
    if(airportCode == null) {
        if(port.airportCode != null)
            return false;           // "Mine is null, theirs is not."
    }
    // END
    if(!port.airportCode.equals(airportCode))
        return false;               // "Their code is different from our."



    // IF gates is an array, note that this distorts any ordering that is non-comformant 
    // with the natural order imposed by the compareTo-method

    Object[] otherGates = port.gates;
    Arrays.sort(gates);             // Sort our gates
    Arrays.sort(otherGates);        // Sort their gates
    return Arrays.equals(gates, otherGates); // This may need overwriting the equals 
                                             // method of the gates, if it isn't a String


    // IF gates is a collection, again, may distort orderings

    List<?> otherGates = port.gates;         // Replace the ? with the type
    Comparator<?> c = null;                  // Again, replace the ?. Assign an implementation of Comparator
                                             // if ? does not implement Comparable on its own

    gates.sort(c);                           // Sort our gates
    otherGates.sort(c);                      // Sort their gates
    return gates.equals(otherGates);         // This may need overwriting the equals 
                                             // method of the gates, if it isn't a String


    // Now at last, if the gates are an object all of their own
    Gates otherGates = port.gates;
    return gates.equals(otherGates);         // Needs an overwriting of equals in class Gates
}

请注意,这可以更加简洁,但是以这种方式显示更容易理解,并且由于给出的信息不完整。

答案 1 :(得分:-1)

我认为这就是它的含义:

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Airport) {
            if(this.airportCode.equals(((Airport) obj).getAirportCode())) {
                String[] gs = ((Airport) obj).getGates();
                Arrays.sort(gates);
                Arrays.sort(gs);
                //Compare gates, if not equal, return false, else true
                return gatesAreEqual;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }