Hirdetés

Keresés

Új hozzászólás Aktív témák

  • Ha már MOOC... Van ez a kód:

    public boolean equals(Object compared) {
    // if the variables are located in the same position, they are equal
    if (this == compared) {
    return true;
    }

    // if the compared object is not of type Person, the objects are not equal
    if (!(compared instanceof Person)) {
    return false;
    }

    // convert the object into a Person object
    Person comparedPerson = (Person) compared;

    // if the values of the object variables are equal, the objects are equal
    if (this.name.equals(comparedPerson.name) &&
    this.age == comparedPerson.age &&
    this.weight == comparedPerson.weight &&
    this.height == comparedPerson.height) {
    return true;
    }

    // otherwise the objects are not equal
    return false;
    }

    Itt nem igazán értem, hogy a type cast-ra (Person comparedPerson = (Person) compared;) mi szükség van, hiszen a metódus már megkapja a compared objektumot :U

    Ha megnézed a metódus paraméterét, akkor láthatod, hogy Object típussal érkezik be. Ahhoz hogy össze tud hasonlítani a különböző Person specifikus fieldeket, először le kell castolnod, hogy elkérhesd őket.

Új hozzászólás Aktív témák