How do I distinguish a global variable from a loca

2019-02-25 01:56发布

问题:

I'm learning Kotlin, and in my project I have something like the following

Utils.kt:

var weightInKilos = 100.0

//should multiply the above var
fun doSomething(multiplier: Double, weightInKilos: Double) {
    weightInKilos = weightInKilos * multiplier
}

print(doSomething(4.2, weightInKilos))

This would be the entire file (it's not part of an object,) so I can't use the this keyword. I know I could just rename one of them but is there some kind of identifier I can use to distinguish the two vars so the code prints 420?

回答1:

Use the package name as the identifier. If the enclosing package for Utils.kt file is com.example, you would use com.example.weightInKilos = weightInKilos * multiplier.

Thank you Android Studio autocomplete.



标签: kotlin