right way to incorporate superclass into a Guava Objects.hashcode() implementation?

前端 未结 3 1127
别跟我提以往
别跟我提以往 2021-02-12 10:44

Possibly a dumb question, but I don\'t want to screw this up. Let\'s say I have two Java classes, Class1 and Class2, where Class2 extends Class1

3条回答
  •  名媛妹妹
    2021-02-12 11:15

    Although Bozho's suggestion is valid, I prefer this approach:

    @Override
    public int hashCode() {
        return Objects.hashcode(mField1, getParentField1(), getParentField2());
    }
    

提交回复
热议问题