Advertisement
khoi01

Example of First Constructor and Second Constructor

Nov 22nd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.46 KB | None | 0 0
  1.  
  2. fun main(args: Array<String>){
  3.  
  4.     var student = Student("Khoi")
  5.  
  6.     student.Name
  7.     println( "value"+student.id)
  8. }
  9.  
  10. //first constructor with properties
  11. class Student(var Name:String){
  12.     //properties for secondary constructor
  13.     var id:Int = -1
  14.  
  15.     init{
  16.         println("Student name is $Name")
  17.     }
  18.  
  19.     //secondary constructor
  20.     //this - refer to 1st constructor
  21.     constructor(name:String,id:Int):this(name){
  22.         this.id=id
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement