search

Home  >  Q&A  >  body text

关于java中重写.equals()方法的问题

ringa_leeringa_lee2809 days ago870

reply all(2)I'll reply

  • 阿神

    阿神2017-04-18 10:46:44

    is the getClass() method of the current object.
    Using methods or properties of the current object within the current object can be ignored this,除非有参数名重复,才需要特别指定 this

    class Demo {
        private String id;
        private String name;
        
        public Demo(String id, String name){
           this.id = id; // 参数与字段名重复,所以需要指定 this
           this.name = name;
        }
        
        public Demo(String username) {
           name = username; // 这里的name 就是当前对象的name
           id = getClass().toString(); //  这里的 getClass() 也是当前对象的getClass() 方法
        }   
    }

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:46:44

    Every time a member method is called, it will be passed in implicitlythis对象,编译器会去给getClass() 加上this

    reply
    0
  • Cancelreply