简体   繁体   中英

Overriding the equals method in Java with objects

I'm currently practicing with object oriented programming, and I've stumbled across on using Override in my program. Over here I want to override the equals method in the class Car and use ONLY plateNumber to determine whether a Car is the same (equal).

Although it is giving the desired output, it isn't using boolean result And I had to use CharSequence in order to compare it. How can I use string compare in this case?

class Car {
    private String make;
    private String model;
    private String plateNumber;
    private int horsePower;

    public Car(String make, String model, String plateNumber, int horsePower) {
        this.make = make;
        this.model = model;
        this.plateNumber = plateNumber;
        this.horsePower = horsePower;
    }


    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        this.plateNumber = plateNumber;
    }

    public int getHorsePower() {
        return horsePower;
    }

    public void setHorsePower(int horsePower) {
        this.horsePower = horsePower;
    }

    @Override
    public boolean equals(Object o) {

        if (o == this)
            return true;

        if (!(o instanceof Car))
            return false;


        Car that = (Car) o;

        return CharSequence.compare(plateNumber, that.plateNumber) == 0;
    }
}

class Main {
    public static void main(String args[]) {

        Car car1 = new Car("Toyota", "RAV4", "ABC-123", 104);
        Car car2 = new Car("Renault", "Megane", "DEF-789", 132);
        Car car3 = new Car("Ford", "Mondeo", "GHI-012", 132);
        Car car4 = new Car("Mercedes", "C180", "ABC-123", 144);


        if (car1.equals(car4)) {
            System.out.println("Equal ");
        } else {
            System.out.println("Not Equal ");
        }

        boolean result = car1.equals(car4);// this should return true because the plateNumbers are the same
        result = car1.equals(car2); // this should return false because the plateNumbers don't match
    }
}

Every Object in Java has hashCode() method which returns an int (a representation of the object).

In your case you want to compare licencePlate hashCodes such as:

@Override
public boolean equals(Object o) {

    if (o == this)
        return true;

    if (!(o instanceof Car))
        return false;

    Car that = (Car) o;
    return Objects.equals(this.hashCode(), that.hashCode());
}

@Override
public int hashCode() {
    return this.plateNumber.hashCode();
}

Now to compare different fields you will need prime number (this post explains clearly why)

@Override
public int hashCode() {
    int prime = 31; //Prime1
    int hash = 7; //Prime2
    hash = prime * hash + (int) id;
    hash = prime * hash + (plateNumber == null ? 0 : plateNumber.hashCode());
    hash = prime * hash + (make == null ? 0 : make .hashCode());
    return hash;
}

Useful link ( Baeldung Tutorial )

You can just override the equals class like this:

@Override
public boolean equals(Object o) {

    if (o == this)
        return true;

    if (!(o instanceof Car))
        return false;


    Car that = (Car) o;

    return this.plateNumber.equals(that.plateNumber);
}

You have to use

@Override
public boolean equals(Object o) {

    if (o == this)
        return true;

    if (!(o instanceof Car))
        return false;


    Car that = (Car) o;

    return this.plateNumber.equals(that.plateNumber);
}

您只需执行plateNumber.equals(that.plateNumber)

package Lesson9.streamandoperations;

import java.util.Objects;

class Car {
    private String make;
    private String model;
    private String plateNumber;
    private int horsePower;

    public Car(String make, String model, String plateNumber, int horsePower) {
        this.make = make;
        this.model = model;
        this.plateNumber = plateNumber;
        this.horsePower = horsePower;
    }


    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        this.plateNumber = plateNumber;
    }

    public int getHorsePower() {
        return horsePower;
    }

    public void setHorsePower(int horsePower) {
        this.horsePower = horsePower;
    }


    @Override
public int hashCode() {

    return Objects.hash(plateNumber);
}

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Car other = (Car) obj;
        if (plateNumber == null) {
            if (other.plateNumber != null)
                return false;
        } else if (!plateNumber.equals(other.plateNumber))
            return false;
        return true;
    }

//    @Override
//    public boolean equals(Object o) {
//
//        if (o == this)
//            return true;
//
//        if (!(o instanceof Car))
//            return false;
//
//
//        Car that = (Car) o;
//
//        return CharSequence.compare(plateNumber, that.plateNumber) == 0;
//    }


}

class Main {
    public static void main(String args[]) {

        Car car1 = new Car("Toyota", "RAV4", "ABC-123", 104);
        Car car2 = new Car("Renault", "Megane", "DEF-789", 132);
        Car car3 = new Car("Ford", "Mondeo", "GHI-012", 132);
        Car car4 = new Car("Mercedes", "C180", "ABC-123", 144);


        if (car1.equals(car4)) {
            System.out.println("Equal ");
        } else {
            System.out.println("Not Equal ");
        }

        boolean result = car1.equals(car4);// this should return true because the plateNumbers are the same
        result = car1.equals(car2); // this should return false because the plateNumbers don't match
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM