How to print the address of an object if you have redefined toString method

后端 未结 5 2120
梦毁少年i
梦毁少年i 2020-12-05 02:18

I\'m a newbie to Java. Now I\'m studying equals and == and redefinition of equals and toString.

I would like to use both the toString method that I have redefied and

5条回答
  •  感情败类
    2020-12-05 02:29

    You can call super() method to execute the corresponding superclass method.

    class Employee{
    ...
        public String toString(){
             String s = super.toString();
            return getClass().getName() + "[name = " + name + 
                ", salary=" + salary + ", hireDay=" + hireDay + "]" + s;
        }
    

    toString() in Object class is as follows

    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    

提交回复
热议问题