먼저! 위에 5가지 사항을 범위 지정 함수 라고 한다. 일단, 크게 3가지로 나눠서 보면 된다. with , apply also , let run (apply, also 는 거의 비슷, let과 run도 거의 비슷하니 서로 비교하면 이해하기 쉽다) 1. with 객체를 만들고 반환. 그 이후에 그 객체의 중복 사용이 있는 경우 - before Class Person { var name: String? = null var age: Int? = null } val person: Person = getPerson() print(person.name) print(person.age) - after val person: Person = getPerson() with(person) { print(name) prin..
1. Data Class - java 변수에 따른 Getter,Setter, 그리고 부가적인 toString(), hashCode(), equals() 함수들도 작성해야 한다. public final class UserInfo { private String name; private int age; public UserInfo(String name, int age) { this.name = name; this.age = age; } /* Getter, Setter */ public String toString() { return "UserInfo(name =" + this.name + ", age=" + this.age + ")"; } public int hashCode() { return (this.na..
1. companion object class 내에 정의할 수 있음 java에서처럼 Class.TYPE 형태로 직접 접근 가능 static은 아님 companion 변수는 private static final 이다. 따라서 외부 접근이 가능하도록 public final get*() 메서드가 생성된다. (아래 이미지 참조) class Sample { val name: String = "Name" companion object { val type: Int = 0 fun isTypeZero(): Boolean { return type == 0 } } } 2. companion object 접근하기 - kotlin vs java kotlin java // 변수 접근 Sample.type // function 접..
1. constructor - primary 생성자 class UserInfo constructor(val name: String, val age: Int, val birthday: String) primary 생성자의 constructor 는 생략 가능 val/ var 로 전역 변수 선언 가능 - secondary 생성자 class UserInfo { constructor(name: String, age: Int) } val/ var 로 전역 변수 선언 할 수 없다 따라서 아래와 같이 코드를 작성해야 한다. class UserInfo { var name: String = "" var age: Int = 0 constructor(name: String, age: Int) { this.name = name..
1. Inheritance (상속) 1-1. open class Kotlin 클래스는 기본적으로 java의 final 이 붙어있다. 그래서 상속 가능한 클래스를 만드려면 open 키워드를 붙여야 한다. (참고로 final 클래스는 상속받을 수 없음을 의미한다.) open class Empty(a: Int) class Sample(a: Int) : Empty(a) 1-2 primary, secondary constructer java코드의 생성자 오버로딩 호환성을 지닌 kotlin constructor 선언 방법 1> class CustomConstraintLayout( context: Context, attrs: AttributeSet?, defStyleAttr: Int) : ConstraintLayou..
1. Nullable 자바와의 비교 void setInfo(@NonNull String a, @Nullable String b) { } public void test() { setInfo(null, null); } 자바는 @NonNull (또는 @NotNull) 을 사용할 수 있다. 그러나 런타임시 Exception 에러를 발생하고, 컴파일단에서는 Warning만 줄 뿐이다. Kotlin에서는 - 기본적으로 null을 허용하지 않는다. - null을 명시적으로 나타내기 위해서는 ? 를 추가해야 한다. - java 디컴파일시 Exception이 발생하도록 내부 처리를 하고 있다. - IDE에서 null을 사용할 수 없음을 즉시 알려준다. fun setInfo(a: String, b: String?) { }..