Kotlin 7

Kotlin에서 변수 깊은 복사(Deep copy) 하기

Gson을 아래와 같이 이용하면 의도적으로 깊은 복사가 가능하다. class Animal{ fun clone(): Animal { val stringAnimal = Gson().toJson(this, Animal::class.java) return Gson().fromJson(stringAnimal, Animal::class.java) } } val originalAnimal = Animal() val clonedAnimal = originalAnimal.clone() 단어. 얕은 복사(shallow copy), 깊은 복사(deep copy) 참고 : https://velog.io/@ellyheetov/Shallow-Copy-VS-Deep-Copy 출처 : https://stackoverflow.com/..

@JvmOverloads: constructor 가 해주는 것

@JvmOverloads 어노테이션을 사용하면 여러 개의 constructor를 상속받는 번거로움이 줄어들고, xml inflation 시에도 문제가 발생하지 않는다. constructor를 직접 작성한 버전 class CustomView: View { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context, attrs: AttributeSet?, @AttrRes defStyleAttr: Int) : super(context, attrs, defStyleAttr) } @JvmOverloads..

Volatile in Kotlin

@Volatile var tmpEndedAt: Long? = null 이렇게 사용하면 됩니다. Volatile이 뭔지 모르신다면,,, 무시 하셔도 됩니다.-0-;; ..... 아니면.. 아래 참고를 보세요. Volatile 참고 : blog.naver.com/PostView.nhn?blogId=classic2u&logNo=50003118713&parentCategoryNo=&categoryNo=15&viewDate=&isShowPopularPosts=false&from=postView m.blog.naver.com/PostView.nhn?blogId=kyed203&logNo=220053808209&proxyReferer=https:%2F%2Fwww.google.com%2F 출처 : stackoverflo..

Kotlin : object keyword vs companion object keyword, and 익명 클래스

object 키워드 - 싱글턴(singletion; 인스턴스가 하나만 있는 클래스)을 선언하는 키워드 object MySingleton{ val prop = "나는 MySingleton의 속성이다." fun method() = "나는 MySingleton의 메소드다." } Companion object 키워드 - 클래스내의 객체 클래스(static이 아니다.... 하지만 비슷한 효과가 난다.) - 축약형이 가능 class MyClass2{ companion object{ val prop = "나는 Companion object의 속성이다." fun method() = "나는 Companion object의 메소드다." } } fun main(args: Array) { println(MyClass2.Com..

Kotlin constructor 정리(코틀린 생성자 정리)

코드를 작성하다보니 아래와 같이 변수 선언에 관한것은 이해가 갔다. var class1 = A()// instance 선언 var class2 : A// 그냥 변수 선언(초기화 안됨) 근데.. 코드를 보다 보면 아래와 같은 코드가 나온다.. 이게 무슨 차일까...?? class A의 input를 받아서 class C로 넘겨 주는것 같은데.. input의 타입도 없고.. 뭐야.. 시부렁.. class C(var input: Int) : A(input) {} class D(var input: Int) : A() {} 여러가지 포스팅과 삽질을 하다가 모든게 코틀린의 복잡한 constructor에서 비롯된다는 것을 알게 되었다. 일단 코틀린은 아래와 같은 규칙(문법)이 존재한다.(근데 잘 안 알려줌-_-) 1...

if(variable == null) on Kotlin

Kotlin에서 null 체크는 어떻게 할 지 궁금해졌다. 아마.. if(variable === null) 이 맞는것이겠지..? 그래서 찾아 보니.. if(variable == null) 을 쓰면 자동으로 if(variable === null) 변환해준다고 한다. Note that there's no point in optimizing your code when comparing to null explicitly: a == null will be automatically translated to a === null. 결론, if(variable == null) 과 if(variable === null) 둘 다 null 체크하는 것이다. 출처 : kotlinlang.org/docs/reference/equ..