Skip to content

Commit

Permalink
kotlin 예제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoldu committed Aug 25, 2024
1 parent b6222e2 commit fcf8511
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion posts/TS를_만나고/1_getter_setter/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 1. TS를 만나고 - 좋은 디자인


```java
public class Course {
private int price;
Expand Down Expand Up @@ -49,4 +50,27 @@ public class Course {
}
}
}
```
```

```kotlin
class Course(private var _price: Int) {

var price: Int
get() = _price
set(value) {
if (value > 0) { // 간단한 검증 논리
_price = value
} else {
throw IllegalArgumentException("Price must be greater than 0")
}
}
}
```

다음과 같이 굳이 별도의 인터페이스 변경 없이 사용 가능하다.

```kotlin
course.price = 200
val coursePrice = course.price
```

0 comments on commit fcf8511

Please sign in to comment.