안드로이드/코틀린

[Android Studio] 코틀린 클래스 오버로딩 커스텀뷰 Class Overloading Constructor Kotlin

eqrw105 2021. 4. 19. 20:29

[Android Studio] 코틀린 클래스 오버로딩 커스텀뷰 Class Overloading Constructor Kotlin

 

1. Java -> Kotlin

public class Pen extends View{

    public Pen(Context context) {
        super(context);
        init(context);
    }

    public Pen(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Pen(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

}

 

open class DrawViewT: View {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super (context, attrs, defStyleAttr)
}

 

2. Java -> Kotlin

 

public class Pen {
    public int x, y;
    
	public Pen(){
    
    }
    
    public Pen(int x, int y){
        this.x = x;
        this.y = y;
    }

}

 

open class Pen() {

    open var x by Delegates.notNull<Float>()
    open var y by Delegates.notNull<Float>()
        
    constructor(x: Float, y: Float): this(){
        this.x = x
        this.y = y
    }
 }