@override annotation

后端 未结 6 1078
别跟我提以往
别跟我提以往 2021-01-17 11:57

Can anyone tell me if this code:

public class OvTester {
    @Override
    public int hashCode() {
        return toString().hashCode();
    }
}
6条回答
  •  执念已碎
    2021-01-17 12:23

    That code overrides the hashCode() method form the base Object class. The toString() method still has the original implementation.

    To override the toString(), you do the following:

    @Override
    public String toString() {
        //Your own toString() implememntation here
    }
    

    As long as the method in the child class has the same name and signature as the method in the parent class, AND the method in the parent class is NOT private it will be overidden(regardless of the presence of the annotation @Override)

提交回复
热议问题