2016년 4월 21일 목요일

kotlin programming language - 클래스 생성

클래스의 생성
코틀린에서는 new 선언자가 없다. 클래스명()으로 선언한다.
class Example(){} -> Example()

생성자
primary constructor와 한개 또는 그 이상의 secondary constructor를 갖는다.
primary constructor : 클래스 헤더에 선언
class constructor Example(title:String){}
단, 다른 annotation이나 visible modifier가 없다면 생략 가능

코클린의 모든 클래스는 공통 클래스 Any를 갖는다. 이것은 기본 선언없이 사용되며 java.lang.Object는 아니다.
클래스를 상속하기 위해서는 'open' annotaion을 붙여준다. 이는 final과 반대되는 성격의 지시자 이다. 다음과 같이 사용한다.

open class Base(p: Int){}
class Derived(p: Int) : Base(p){}

method overrinde
java와 동일하게 사용된다. super class에 동일한 메소드가 있다면 override annotation을 앞에 붙인다.
super class의 멤버에 open으로 선언되어 있어야 한다.
open class Base {
  open fun v() {}
  fun nv() {}}class Derived() : Base() {
  override fun v() {}}


gettter&setter

-작성중-

댓글 없음:

댓글 쓰기