Pages

2022年6月16日星期四

Variable Shadowing & 'this' Keyword

 Take a look at this piece of code




For those who remember boiler plate code, this code will looks strange because it hasn't 'this' keyword. But this code works fine


this keyword only needed and become obligatory when facing variable shadowing.



This is variable shadowing. The constructor method will never be able to modify class attributes, because attributes name is the same with parameter name (the variable name overridden). Therefore you need to use this keyword, so the compiler could distinguish the address belongs to the parameter, and which one belongs to the attributes

Note: When calling instance method, people usually use this keyword. It is not necessary actually


没有评论:

发表评论