Published: 07 Mar 2018 › Updated: 07 Mar 2018![[Kotlin] getter/setter를 설정해야 하는 귀찮음 제거 data class](/nhc-explorer-cover.webp)
![[Kotlin] getter/setter를 설정해야 하는 귀찮음 제거 data class](/nhc-explorer-cover.webp)
[Kotlin] getter/setter를 설정해야 하는 귀찮음 제거 data class
안녕하세요. 자유입니다.
요즘 대세인 코트린학습중에 편리한 기능을 소개하고자 합니다.
Retrofit을 이용해서 json을 파싱하기 위해서는 DTO Pojo 클래스를 설정해야 합니다.
보통 Java에서 설정을 할때
class Artist{
private long id;
public Artist(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Artist artist = new Artist(500);
artist.getId();
}
}
보통 이렇게 class선언후 사용을 해야 합니다.
코트린은?
data class Artist(var id:Long)
class Request(val url : String){
fun run(){
val artist : Artist = Artist(500)
artist.id
}
}
끝...
data class로 선언만 해주면 생성자, getter,setter다 됩니다.
끝.
Leave [Kotlin] getter/setter를 설정해야 하는 귀찮음 제거 data class to:
Read more #kotlin posts
Best Posts From myeoungwon.kang
We have not curated any of myeoungwon.kang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.