Skip to content

Commit

Permalink
[Week1][입문편] 14강 코틀린에서 다양한 클래스를 다루는 방법 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Jun 21, 2024
1 parent 675ed1e commit acdc803
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 3 deletions.
6 changes: 3 additions & 3 deletions 임준형/kotlin-grammer/src/lec09/lec09.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Person(
println("두 번째 부 생성자")
}

fun isAdult(): Boolean {
return this.age >= 20
}
// fun isAdult(): Boolean {
// return this.age >= 20
// }

val isAdult: Boolean
get() = this.age >= 20
Expand Down
17 changes: 17 additions & 0 deletions 임준형/kotlin-grammer/src/lec14/Country.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lec14

import lec14.Country.*

fun handleCountry(country: Country) {
when (country) {
KOREA -> TODO()
AMERICA -> TODO()
}
}

enum class Country(
private val code: String
) {
KOREA("KO"),
AMERICA("US")
}
29 changes: 29 additions & 0 deletions 임준형/kotlin-grammer/src/lec14/JavaCountry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lec14;

public enum JavaCountry {

KOREA("KO"),
AMERICA("US"),

;

private final String code;

JavaCountry(final String code) {
this.code = code;
}

public String getCode() {
return code;
}

private static void handleCountry(JavaCountry country) {
if (country == JavaCountry.KOREA) {
// 로직 처리
}

if (country == JavaCountry.AMERICA) {
// 로직 처리
}
}
}
47 changes: 47 additions & 0 deletions 임준형/kotlin-grammer/src/lec14/JavaPersonDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lec14;

import java.util.Objects;

public class JavaPersonDto {

private final String name;
private final int age;

public JavaPersonDto(final String name, final int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
JavaPersonDto that = (JavaPersonDto) o;
return getAge() == that.getAge() && Objects.equals(getName(), that.getName());
}

@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}

@Override
public String toString() {
return "JavaPersonDto{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
16 changes: 16 additions & 0 deletions 임준형/kotlin-grammer/src/lec14/PersonDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lec14

fun main() {
val dto1 = PersonDto("임준형", 100)
val dto2 = PersonDto("임준형", 200)
val dto3 = PersonDto("임준형", 200)
println(dto1 == dto2)
println(dto2 == dto3)
println(dto3)
}

data class PersonDto(
val name: String,
val age: Int
) {
}
39 changes: 39 additions & 0 deletions 임준형/kotlin-grammer/섹션3 코틀린에서의 OOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,42 @@ inner는,, 권장하니 않으니 알아보지도 말자
### Kotlin
| 클래스 안의 클래스 | 바깥 클래스 참조 없음<br/> 권장되는 유형 |
|-----------------|----------------------------------------------|


## 14강 코틀린에서 다양한 클래스를 다루는 방법

### data class

Kotlin에서 data 키워드를 붙여주면 Java에서 `toString`, `equals`, `hashCode`을 쓰는 것과 동일하다

사실상 namedQuery를 사용하면 Builder 또한 사용 가능

### enum class

Java와 Kotlin은 동일. 하지만 when을 쓰면 강력해짐

### Java
```java
private static void handleCountry(JavaCountry country) {
if (country == JavaCountry.KOREA) {
// 로직 처리
}

if (country == JavaCountry.AMERICA) {
// 로직 처리
}
}
```

### Kotlin
```kotlin
fun handleCountry(country: Country) {
when (country) {
KOREA -> TODO()
AMERICA -> TODO()
}
}
```

else 처리를 하지 않아도됨. 이유는? 어차피 지금 Country에는 KOREA, AMERICA 밖에 없기 떄문에, 별도의 예외처리를 하지 않아도 됨.

0 comments on commit acdc803

Please sign in to comment.