📂Kotiln21_OverridingEx1 ~ 📂Kotlin26_InterfaceEx1
Kotlin에서 사용하는 모든 클래스의 부모 클래스
fun main() {
var a1: Any = TestClass1()
var a2: Any = TestClass2()
}
class TestClass1
class TestClass2
fun main() {
val t1 = TestClass(100, 200)
println(t1)
}
class TestClass (var a1: Int, var a2: Int) {
override fun toString(): String {
}
}
📂Kotlin22_Any
class TestClass {
private var num: Int = 0
fun setNum(num: Int) {
this.num = num // 멤버 변수에 접근하기 위해 this 키워드 사용
}
fun printNum() {
println("My variable: $num") // 멤버 변수에 직접 접근
}
}
fun main() {
val obj = TestClass()
obj.setNum(10)
obj.printNum()
}
fun main() {
val obj = TestClass()
obj.testMethod()
}
class TestClass {
fun testMethod() {
println("testMethod")
this.internalMethod() // 메서드 내부의 메서드 호출
}
private fun internalMethod() {
println("internalMethod")
}
}
fun main() {
val obj1 = TestClass("John", 25)
val obj2 = TestClass("Alice")
}
class TestClass(val name: String, val age: Int) {
constructor(name: String) : this(name, 0) {
println("Secondary constructor: name=$name")
}
init {
println("Primary constructor: name=$name, age=$age")
}
}
➡️ Primary constructor: name=John, age=25
Primary constructor: name=Alice, age=0
Secondary constructor: name=Alice
fun main() {
val obj = SubClass()
obj.printNum()
}
open class SuperClass {
open val num: Int = 5
}
class SubClass : SuperClass() {
override val num: Int = 10
fun printNum() {
println("SubClass - num : $num")
println("SuperClass - num : ${super.num}")
}
}
fun main() {
val obj = SubClass()
obj.testMethod()
}
open class SuperClass {
open fun testMethod() {
println("SuperClass - testMethod")
}
}
class SubClass : SuperClass() {
override fun testMethod() {
super.testMethod() // 부모 클래스의 메서드 호출
println("SubClass - testMethod")
}
}
➡️ SuperClass - testMethod
SubClass - testMethod
fun main() {
val obj = SubClass("John", 25)
obj.printName()
}
open class SuperClass(val name: String) {
open fun printName() {
println("SuperClass - Name : $name")
}
}
class SubClass(name: String, val age: Int): SuperClass(name) {
override fun printName() {
super.printName() // 부모 클래스의 메서드 호출
println("SubClass - Age : $age")
}
}
➡️ SuperClass - Name: John
SubClass - Age: 25
추상 클래스 : 추상 메서드를 가지고 있는 클래스
추상 메서드 : 구현되지 않은 메서드
abstract
와 상속을 위해 open
키워드 사용fun main() {
val t2 = TestClass2()
t2.testMethod()
val t3: TestClass1 = TestClass2()
t3.testMethod()
}
// 추상 클래스
open abstract class TestClass1 {
// 추상 메서드
open abstract fun testMethod()
}
// 추상 클래스를 상속 받은 클래스
class TestClass2 : TestClass1() {
override fun testMethod() {
println("TestClass2의 testMethod")
}
}
➡️ TestClass2의 testMethod
TestClass2의 testMethod
📂Kotlint23_Abstract
인터페이스를 사용하여 다양한 참조변수에 담을 수 있는 클래스를 만들 수 있음
fun main() {
val t1 = TestClass()
t1.interfaceMethod1()
t1.interfaceMethod2()
t1.interfaceMethod3()
testFunction1(t1)
testFunction2(t1)
}
fun testFunction1(t100: Inter1) {
t100.interfaceMethod1()
}
fun testFunction2(t200: Inter2) {
t200.interfaceMethod2()
}
interface Inter1 {
fun interfaceMethod1()
}
interface Inter2 {
fun interfaceMethod2()
}
interface Inter3 {
fun interfaceMethod3()
}
// 인터페이스를 정의한 클래스
class TestClass : Inter1, Inter2, Inter3 {
override fun interfaceMethod1() {
println("interfaceMetho1")
}
override fun interfaceMethod2() {
println("interfaceMetho2")
}
override fun interfaceMethod3() {
println("interfaceMethod3")
}
}
📂Kotlin25_Interface