Class

Member Function

class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
  init {
println("$name has ${estimatedGraduationYear -2020} years left in college.")
  }
  //member function
  fun calculateLetterGrade(): String {
    return when {
      gpa >= 3.0 -> "A"
      gpa >= 2.7 -> "B"
      gpa >= 1.7 -> "C"
gpa >= 1.0 -> "D"
      else -> "E"
    }
  }
}
//When the instance is created and the function is called, the when expression will be executed and return the letter grade
fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022)
//Prints: Lucia has 2 years left in college.
  println("${student.name}'s letter grade is ${student.calculateLetterGrade()}.")
  //Prints: Lucia's letter grade is A.
}

Initialization Block

class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
  init {
    println("$name has ${estimatedGraduationYear -2020} years left in college.")
  }
}
fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022)
//Prints: Lucia has 2 years left in college. 
}

Primary Constructor

class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) 
fun main() {
  var student = Student("Lucia", 3.95, "Fall", 2022) 
  println(student.name)     
  //Prints: Lucia
  println(student.gpa)
  //Prints: 3.95
  println(student.semester) 
  //Prints: Fall
  println(student.estimatedGraduationYear) 
  //Prints: 2022
}

Class Instance

// Class
class Student {
  var name = "Lucia"
  var semester = "Fall"
var gpa = 3.95
}
fun main() {
  var student = Student()   
  // Instance
  println(student.name)     
  // Prints: Lucia
  println(student.semester) 
  // Prints: Fall
  println(student.gpa)      
  // Prints: 3.95 
} 

Class Example

//class with properties containing default values
class Student {
  var name = "Lucia"
  var semester = "Fall"
  var gpa = 3.95
}
//shorthand syntax without class body
class Student
Comments