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

前端 未结 3 1093
别跟我提以往
别跟我提以往 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:23

    Yes, that looks correct. It would be the same if you had Objects.hashCode(f1, f2, f3, f4). If you look at the implementation, it's something like result += 31 * result + hashcodeOfCurrentObject. Which means that your result will be 31 + the super hashcode, which is not exactly the same, but would not be a problem.

提交回复
热议问题