3.9 this Keyword

0 阅读1分钟

1. Exam Points

  • this keyword refers to the current object of a class.
  • this can be used to distinguish an instance variable from a local variable with the same name.
  • this can be used to pass the current object as an argument to a method.
  • this can not be used in a static method.
  • Note:
    • pay attention to in which class this keyword is used.

2. Knowledge Points

(1) this Keyword

  • Within an instance method or a constructor, the keyword this acts as a special variable that holds a reference to the current object — the object whose method or constructor is being called.
  • Class methods do not have a this reference, meaning that you can not use the this keyword in a class method.
  • this keyword can be used to distinguish an instance variable from a local variable with the same name.
    • Example:
      image.png
  • The keyword this can be used to pass the current object as an argument in a method call.
    • Example:
      image.png

3. Exercises