https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

skwalking

07/21/2021, 9:21 AM
Hi I was trying to understand the flow in below code:
Copy code
1 class Person(val pets: MutableList<Pet> = mutableListOf())
2
3 class Pet {
4     constructor(owner: Person) {
5         owner.pets.add(this) // adds this pet to the list of its owner's pets
6     }
7 }
Can someone please use above code to make an example on printing something. Since I'm unable to understand the flow of the code I'm also unable to find what "this" in code line no. 5 does. Thank you 🙂
c

crummy

07/21/2021, 9:54 AM
in a
class
,
this
refers to the instance of the class
h

hho

07/21/2021, 9:58 AM
https://pl.kotl.in/gLxVWuNfk From the IDs you can tell the different instances apart.
2 Views