클래스 만들기

class Dog{
  fun bark() = "bark"
}

fun main(){
  val dog = Dog()
}

프로퍼티

프로퍼티는 클래스에 속한 var, val이다.

class Cup{
  var percentFull = 0
  val max = 100
  fun add(increase: Int): Int{
    percentFull += increase
    if(percentFull > max)
      percentFull = max
    return percentFull
  }
}

fun main() {
  val cup = Cup()
  cup.add(50)
  println(cup.percentFull)
  cup.add(70)
  println(cup.percentFull)
}

생성자

class Wombat

fun main(){
  val wombat = Wombat()
}

class Alien(name: String){
  val greeting = "Poor $name!"
}

fun main(){
  val alien = Alien("Mr.Cho")
  println(alien.greeting)
}

class Alien2(val name: String)

fun main(){
  val alien2 = Alien2("Mr.Cho")
  println(alien2.name)
}

댓글남기기