Skip to content

Commit

Permalink
[Week1][입문편] 12강 코틀린에서 object 키워드를 다루는 방법 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Jun 12, 2024
1 parent 420de03 commit aac3f47
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lec12.objectANDsingleton;

public class JavaPerson {
private static final int MIN_AGE = 1;

private String name;
private int age;

private JavaPerson(final String name, final int age) {
this.name = name;
this.age = age;
}

public static JavaPerson from(final String name) {
return new JavaPerson(name, MIN_AGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lec12.objectANDsingleton;

public class JavaSingleton {
private static final JavaSingleton INSTANCE = new JavaSingleton();

private JavaSingleton() {
}

public static JavaSingleton getInstance() {
return INSTANCE;
}
}
5 changes: 5 additions & 0 deletions 임준형/kotlin-grammer/src/lec12/objectANDsingleton/Log.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lec12.objectANDsingleton

interface Log {
fun log();
}
26 changes: 26 additions & 0 deletions 임준형/kotlin-grammer/src/lec12/objectANDsingleton/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lec12.objectANDsingleton

class Person private constructor(
private var name: String,
private var age: Int
) {

// static 대신 companion object 사용
// const 작성시 컴파일 시에 값이 들어감. 정말 진짜 상수
// val 작성시 런타임 시에 값이 들어감.
companion object Factory : Log {
private const val MIN_AGE = 1
fun newBaby(name: String): Person {
return Person(name, MIN_AGE)
}

override fun log() {
println("나는 Person 클래스 동행 객체에요")
}
}
}

// 외부에서 바로 접근 가능
object Singleton {
var a:Int = 0
}
23 changes: 23 additions & 0 deletions 임준형/kotlin-grammer/src/lec12/staticclass/JavaMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lec12.staticclass;

public class JavaMain {

public static void main(String[] args) {
moveSomething(new Moveable() {
@Override
public void move() {
System.out.println("움직인다");
}

@Override
public void fly() {
System.out.println("난다");
}
});
}

private static void moveSomething(Moveable moveable) {
moveable.move();
moveable.fly();
}
}
22 changes: 22 additions & 0 deletions 임준형/kotlin-grammer/src/lec12/staticclass/Lec12Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lec12.staticclass

import lec12.objectANDsingleton.Person

fun main() {
moveSomething(object : Moveable {
override fun move() {
println("움직인다")
}

override fun fly() {
println("난다")
}
})

Person.newBaby("hello")
}

private fun moveSomething(moveable: Moveable) {
moveable.move()
moveable.fly()
}
8 changes: 8 additions & 0 deletions 임준형/kotlin-grammer/src/lec12/staticclass/Moveable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package lec12.staticclass;

public interface Moveable {

void move();

void fly();
}
44 changes: 44 additions & 0 deletions 임준형/kotlin-grammer/섹션3 코틀린에서의 OOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,48 @@ class Lec11Main {
isDirectoryPath("/")
}
}
```

## 12강 코틀린에서 object 키워드를 다루는 방법

Java에서 static 클래스를 Kotlin에선 아래와 같이 `companion object`표현

```kotlin
companion object Factory : Log {
private const val MIN_AGE = 1
fun newBaby(name: String): Person {
return Person(name, MIN_AGE)
}

@JvmStatic
override fun log() {
println("나는 Person 클래스 동행 객체에요")
}
}
```

외부에선 아래와 같이 적용 가능하며 마찬가지로 static import와 유사하게 가능

```kotlin
Person.newBaby("hello")
```

Java에서 컴패니언 사용하려면 @JvmStatic을 붙여야함

### 싱글톤

object 키워드 쓰면 끝

### 익명클래스 구현시

```kotlin
moveSomething(object : Moveable {
override fun move() {
println("움직인다")
}

override fun fly() {
println("난다")
}
})
```

0 comments on commit aac3f47

Please sign in to comment.