please solve | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

please solve

class Num(value:Int) { var v = value+2 set(value) { field = value+v } } fun main(args: Array<String>) { val x = Num(3) x.v = 7 println(x.v) }

13th Jul 2021, 1:44 PM
Amrita Singh
Amrita Singh - avatar
2 Answers
+ 2
Your code have no error little bit tell more about your problems what u want..?
13th Jul 2021, 6:03 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Code works as expected I guess, what there is to solve though? If you need explanation on how the code works, then you should write "How this code work?" rather than "please solve" which indicated there was a problem. class Num( value : Int ) { var v = value + 2 set( value ) { field = value + v } } fun main(args: Array<String>) { val x = Num( 3 ) x.v = 7 println( x.v ) } Object of class Num <x> is constructed & initialized from <value> (3). A member named <v> is initialized by <value> (3) + 2 which yields 5. A property setter named `set` allows member data modification, given a property name. The property setter is invoked on `x.v = 7` in main(). The property setter modifies member <v> (5) by adding the given <value> (7), which effectively changes member <v> value to 12 (7 + 5). I hope it made a bit of sense, cmiiw (Edited)
13th Jul 2021, 3:04 PM
Ipang