When the body of the method
public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; }
Performed
it runs on a specific instance of struct Complex . You can access the instance executed by the code using the this . Therefore you can think of the body of the method
public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; }
like reading
public Complex(int real, int imaginary) { assign the parameter real to the field real for this instance assign the parameter imaginary to the field imaginary for this instance }
There is always an implicit this , so the following equivalents
class Foo { int foo; public Foo() { foo = 17; } } class Foo { int foo; public Foo() { this.foo = 17; } }
However, local residents have priority over members, therefore
class Foo { int foo; public Foo(int foo) { foo = 17; } }
assigns 17 , so the variable foo is a parameter of the method. If you want to assign an instance member, if you have a method where there is a local name with the same name, you should use this to refer to it.
jason source share