ํฐ์คํ ๋ฆฌ ๋ทฐ
[Kotlin] getter, setter์ ๋ํด์ (Effective Kotlin ์คํฐ๋)
pyorolong 2024. 3. 11. 18:33val ๊ทธ๋ฆฌ๊ณ var
getter, setter๋ฅผ ์ด์ผ๊ธฐํ๋๋ฐ ์ฌ val var ์ ์ด์ผ๊ธฐ๋ ์ถ์ ์ ์์ง๋ง ์ ๋๋ก ๊ณต๋ถํด๋ณด๋ ์ด ์ด์ผ๊ธฐ๋ฅผ ํ์ง ์์ ์ ์๋ค.
val, var์ ์๋ฏธ
val : value
var : variable
์ ๋ง ๊ธฐ๋ณธ ์ ์ธ ์ฌ์ค ์ด์ง๋ง ๊ฐ๊ณผํ๊ณ ์์๋ค. ๊ทธ๋์์ ์๋์ ๊ฐ์ด ์๊ณ ์์์ง๋ง ์ฌ์ค ์ด ๋ด์ฉ์ผ๋ก๋ง ์๊ณ ์์๋ค๋ฉด ๋น์ฝ์ด๋ค.
val : immutable
var : mutable
getter setter์ ๊ฐ๋ ๋ ์บก์ํ์ ๊ด๋ จํ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ์ธ๋ฐ, ๊ทธ๋ณด๋ค ๋ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ์ val var ์ด์ผ๊ธฐ๋ฅผ ์ ํ๋๋?
Kotlin์ ๋ณ์์ getter(), setter()๋ฅผ ๋ด๋ถ์ ์ผ๋ก ์์ฑํด์ฃผ๋ ์ฅ์ ์ด ์๋ค. ์ด์ ๊ด๋ จํ์ฌ val, var์ ํต์ฌ์ ์ธ ์ฐจ์ด๋ ๋ฐ๋ก ์ด๊ฒ์ด๋ค.
val : getter ๋ง ์ ๊ณต, var : getter์ setter ๋ชจ๋ ์ ๊ณต
val ์์ฒด๊ฐ immutable ํ๊ณ var ์์ฒด๊ฐ mutable ํ ๊ฒ์ด๋ผ๊ธฐ ๋ณด๋ค๋ setter๊ฐ ์ ๊ณต๋๋, ๋์ง ์๋์ ์ฐจ์ด์์ ๋ฐ์ํ๋ค.
์ด๋ ๊ฒ ๋๋ฌธ์ val๋ก ์ ์๋ ๋ณ์๋ฅผ var๋ก override ํ ์ ์๋ ๊ฒ์ด๋ค.
interface Game {
val name: String
}
class NightCrow(override var name: String) : Game
// Game interface์ val๋ก ์ ์๋ name ๋ณ์๋ฅผ var๋ก override ํ ์ ์๋ค
Getter ๊ทธ๋ฆฌ๊ณ Setter
์์ ๋งํ ๋ฏ Kotlin์ getter()์ setter()๋ฅผ ๋ด๋ถ์ ์ผ๋ก ์์ฑํ๋ค. ์๋ Kotlin ์์ค๋ฅผ Java๋ก Decompileํ ๊ฒฐ๊ณผ์ ๋น๊ตํด๋ณด์.
Kotlin
class GetterSetterTestModel() {
var firstName: String = "HeeDong"
val lastName: String = "Gang"
val fullName: String
get() = firstName + lastName
val fullName1: String = firstName + lastName
}
Java
public final class GetterSetterTestModel {
@NotNull
private String firstName = "HeeDong";
@NotNull
private final String lastName = "Gang";
@NotNull
private final String fullName1;
@NotNull
public final String getFirstName() {
return this.firstName;
}
public final void setFirstName(@NotNull String var1) {
this.firstName = var1;
}
@NotNull
public final String getLastName() {
return this.lastName;
}
@NotNull
public final String getFullName() {
return this.firstName + this.lastName;
}
@NotNull
public final String getFullName1() {
return this.fullName1;
}
public GetterSetterTestModel() {
this.fullName1 = this.firstName + this.lastName;
}
}
๋น๊ต ๊ฒฐ๊ณผ - val, var์ ์ฐจ์ด
Kotlin | java | |
1 | var firstName = "HeeDong" | private String firstName = "HeeDong" public final String getFirstName() { return this.firstName;} public final void setFirstName(@NotNull String var1) { this.firstName = var1;} |
2 | val lastName = "Gang" | private final String lastName = "Gang"; public final String getLastName() { return this.lastName;} |
- Kotlin์ var firstName๋ Java๋ก decompile์ final String getFirstName() ํจ์์
public final void setFirstName()์ getter setter๊ฐ ์์ฑ๋จ - val lastName = "Gang"์ public final String getLastName() getter๊ฐ ์์ฑ๋จ
๋น๊ต ๊ฒฐ๊ณผ - ๊ธฐ๋ณธ getter์ custom getter์ ์ฐจ์ด
Kotlin | java | |
1 | val fullName: String get() = firstName + lastName |
public final String getFullName() { return this.firstName + this.lastName;} |
2 | val fullName1: String = firstName + lastName | public GetterSetterTestModel() { this.fullName1 = this.firstName + this.lastName;} public final String getFullName() { return this.firstName + this.lastName;} |
- val fullName ๊ณผ ๊ฐ์ custom getter๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ฐธ๊ณ ํ๊ณ ์๋ firstName, lastName๋ณ์์ ํ์ฌ ๊ฐ์ ๊ทธ๋๋ก returnํจ
- val fullName1๊ณผ ๊ฐ์ด ๋ณ์๋ก ์ ์ธํ ๊ฒฝ์ฐ GetterSetterTestModel ์์ฑ์์์ ์ด๊ธฐ๊ฐ์ ์ค์ ํ๊ณ , ์ฐธ์กฐํ๊ณ ์๋ ๋ณ์์ ์ด๊ธฐ๊ฐ๋ง์ ์ฐธ๊ณ ํ์ฌ ๊ฐ์ assignํจ
- Total
- Today
- Yesterday
- How to import android AAR file
- aar import
- android databinding
- Hilt
- Support for password authentication was removed on August 13
- The requested URL returned error
- android
- Kotlin
- com.android.build.api.transform.TransformException
- databinding onclick not working
- android aar
- AndroidManifest
- Cannot create an instance
- android aar library
- Please use a personal access token instead
- launchemode
- android launch mode in manifest
- DexArchiveBuilderException
- module-info is missing a super type
- custom setter
- decomplie
- Effective Kotlin
- viewmodel
- import aar
- databinding onClick
- custom getter
- AAR
- Make onClick event in Android databinding
- GitHub
- annotaion
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |