ꡬκΈμμ μ 곡νλ κ΅μ‘μλ£λ₯Ό μ 리νκΈ° μν ν¬μ€νΈμ λλ€.
instances
μμ μλνλ methods
κ° μ μλμ΄ μμ΅λλ€.ν΄λμ€λ₯Ό μ μνκ³ κ°μ²΄μ μλ‘μ΄ instance
λ₯Ό μμ±ν©λλ€.
class House {
val color: String = "white"
val numberOfWindows: Int = 2
val isForSale: Boolean = false
fun updateColor(newColor: String){...}
...
}
val myHouse = House()
println(myHouse)
μμ±μκ° ν΄λμ€ ν΄λμ μ μλ κ²½μ° νλΌλ―Έν°λ₯Ό ν¬ν¨ν μ μμ΅λλ€.
instances
μμ μ κ·Ό κ°λ₯ν©λλ€.class A
val aa = A()
class B(x: Int)
val bb = B(12)
println(bb.x) // compiler error unresolved reference
class C(val y: Int)
val cc = C(42)
println(cc.y) // 42
ν΄λμ€ instances
λ κΈ°λ³Έκ°μ κ°μ§ μ μμ΅λλ€.
class Box(val length: Int, val width:Int = 20, val height:Int = 40)
val box1 = Box(100, 20, 40)
val box2 = Box(length = 100)
val box3 = Box(length = 100, width = 20, height = 40)
ν΄λμ€ ν΄λμμ κΈ°λ³Έ μμ±μλ₯Ό μ μν©λλ€.
class Circle(i: Int) {
init {
...
}
}
μμ λ°©λ²κ³Ό λμΌν κΈ°λ₯μ ν©λλ€.
class Circle {
constructor(i: Int) {
...
}
}
init
λΈλ‘μμ μ²λ¦¬ν μ μμ΅λλ€.init
λΈλ‘λ νμ©λ©λλ€.init
λΈλ‘μ κΈ°λ³Έ μμ±μμ body
κ° λ©λλ€.class Square(val side: Int) {
init {
println(side * 2)
}
}
val s = Square(10) // 20
constructor
ν€μλλ₯Ό μ¬μ©ν΄μ secondary constructors μ μν μ μμ΅λλ€.body
λ νμκ° μλλλ€.class Circle(val radius:Double) {
constructor(name:String) : this(1.0)
constructor(diameter:Int) : this(diameter / 2.0) {
println("in diameter constructor")
}
init {
println("Area: ${Math.PI * radius * radius}")
}
}
val c = Circle(3)
Properties
μ μ μν©λλ€..
μ μ¬μ©νμ¬ Properties
μ μ κ·Όν©λλ€.Properties
λ .
λ‘ μ κ·Όνμ¬ κ°μ μ€μ ν μ μμ΅λλ€.class Person(var name: String)
fun main() {
val person = Person("Alex")
println(person.name) Access with .<property name>
person.name = "Joey" Set with .<property name>
println(person.name)
}
κΈ°λ³Έ get/set μ¬μ©μ μνμ§ μμ κ²½μ° μ¬μ© ν©λλ€.
var propertyName: DataType = initialValue
get() = ...
set(value) {
...
}
class Person(val firstName: String, val lastName:String) {
val fullName:String
get() {
return "$firstName $lastName"
}
}
val person = Person("John", "Doe")
println(person.fullName) // John Doe
var fullName:String = ""
get() = "$firstName $lastName"
set(value) {
val components = value.split(" ")
firstName = components[0]
lastName = components[1]
field = value
}
person.fullName = "Jane Smith"
members
λ₯Ό μμν©λλ€.interface
λ₯Ό μ μν μ μλ€.interface Shape {
fun computeArea() : Double
}
class Circle(val radius:Double) : Shape {
override fun computeArea() = Math.PI * radius * radius
}
val c = Circle(3.0)
println(c.computeArea()) // 28.274333882308138
open
ν€μλλ₯Ό μ¬μ©ν©λλ€.Properties
, functions
μ override
ν€μλλ₯Ό ν΅ν΄ μ¬μ μν©λλ€.class A
class B : A
Error: A is final and cannot be inherited from
open class C
class D : C()
properties
μ methods
μ λν΄ open
μ μ¬μ©ν΄μΌ ν©λλ€.(κ·Έλ μ§ μμ κ²½μ° μ»΄νμΌ μ€λ₯ λ°μ)properties
μ methods
λ₯Ό μ¬μ μ ν λ override
ν€μλλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.override
λ‘ νμλ κ²μ νμ ν΄λμ€μμ μ¬μ μλ μ μμ΅λλ€.(κΈ°λ³Έμ μΌλ‘ open, νμ ν΄λμ€μμ μ¬μ μλ₯Ό λ§κ³ μΆμΌλ©΄ final μΆκ°)abstract
λ‘ νμλμΌ ν©λλ€.abstract
λ‘ νμλ properties
μ methods
λ μ¬μ μν΄μΌ ν©λλ€.abstract
λ‘ νμλμ§ μμ properties
μ methods
λ₯Ό ν¬ν¨ν μ μλ€.abstract class Food {
abstract val kcal : Int
abstract val name : String
fun consume() = println("I'm eating ${name}")
}
class Pizza() : Food() {
override val kcal = 600
override val name = "Pizza"
}
fun main() {
Pizza().consume() // "I'm eating Pizza"
}
properties
μ methods
λ₯Ό νμ ν΄λμ€μμ μ¬μ μλλλ‘ νκ³ μΆμ κ²½μ° μΆμν ν΄λμ€λ₯Ό κ³ λ €ν©λλ€.extend
ν μ μμ§λ§ μ¬λ¬ μΈν°νμ΄μ€λ₯Ό implement
ν μ μμ΅λλ€.μ§μ μμ ν μ μλ κΈ°μ‘΄ ν΄λμ€μ ν¨μλ₯Ό μΆκ°ν©λλ€.
private
μΈμ€ν΄μ€ λ³μμ μ κ·Όν μ μμ΅λλ€.open
λμ§ μμ ν΄λμ€μ κΈ°λ₯μ μΆκ°ν μ μμ΅λλ€.note: ν΄λμ€μ κ°μ νμΌμ΄λ μ μ μλ ν¨μμ κ°μ΄ μ½κ² κ²μν μ μλ μμΉμ νμ₯ν¨μλ₯Ό μ μν©λλ€.
fun Int.isOdd(): Boolean { return this % 2 == 1 }
3.isOdd() // true
data
ν€μλλ‘ νμν©λλ€.toString()
, equals()
, hashCode()
, copy()
λ©μλ λ° λ€μν μ°μ°μλ₯Ό μμ±ν©λλ€.data class Player(val name: String, val score: Int)
val firstPlayer = Player("Lauren", 10)
println(firstPlayer) // Player(name=Lauren, score=10)
.first
, .second
, .third
λ₯Ό μ¬μ©νμ¬ λ³μμ μ κ·Όν©λλ€.val bookAuthor = Pair("Harry Potter", "J.K. Rowling")
println(bookAuthor) // (Harry Potter, J.K. Rowling)
val bookAuthorYear = Triple("Harry Potter", "J.K. Rowling", 1997)
println(bookAuthorYear) // (Harry Potter, J.K. Rowling, 1997)
println(bookAuthorYear.third) // 1997
to
λ₯Ό μ¬μ©νλ©΄ κ΄νΈμ λ§μΉ¨νλ₯Ό μλ΅ν μ μμ΅λλ€.
val bookAuth1 = "Harry Potter".to("J. K. Rowling")
val bookAuth2 = "Harry Potter" to "J. K. Rowling"
Map
λ° HashMap
κ³Ό κ°μ 컬λ μ
μμλ μ¬μ©λ©λλ€.
val map = mapOf(1 to "x", 2 to "y", 3 to "zz")
μ§μ λ κ° μ§ν©μΌλ‘ μ¬μ©μ μ μ λ°μ΄ν° νμ μ λλ€.
enum
ν€μλλ₯Ό μ¬μ©ν©λλ€.enum class Color(val r: Int, val g: Int, val b: Int) {
RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255)
}
println("" + Color.RED.r + " " + Color.RED.g + " " + Color.RED.b) // 255 0 0
object
ν€μλλ₯Ό μ¬μ©ν©λλ€.object Calculator {
fun add(n1: Int, n2: Int): Int {
return n1 + n2
}
}
println(Calculator.add(2,4)) // 6
companion
ν€μλλ₯Ό μ¬μ©ν©λλ€.ClassName.Property
λλ ClassName.Function
μ ν΅ν΄ μ°Έμ‘°λ©λλ€.class PhysicsSystem {
companion object WorldConstants {
val gravity = 9.8
val unit = "metric"
fun computeForce(mass: Double, accel: Double): Double {
return mass * accel
}
}
}
println(PhysicsSystem.WorldConstants.gravity) // 9.8
println(PhysicsSystem.WorldConstants.computeForce(10.0, 10.0)) // 100.0
package
ν€μλ λ€μμ μ€λ νμΌμμ μ£Όμμ΄ μλ 첫 λ²μ§Έ μ½λ μ€μ μ μΈλ©λλ€.