programing

Kotlin에서 동시에 확장 및 구현

goodjava 2023. 1. 4. 20:06

Kotlin에서 동시에 확장 및 구현

Java에서는 다음과 같은 작업을 수행할 수 있습니다.

class MyClass extends SuperClass implements MyInterface, ...

코틀린에서도 같은 일이 가능한가요?가정하다SuperClass추상적이며 구현되지 않습니다.MyInterface

인터페이스 구현과 클래스 상속 사이에는 구문적인 차이가 없습니다.모든 유형을 콜론 뒤에 쉼표로 구분하여 나열하기만 하면 됩니다.:다음과 같이 합니다.

abstract class MySuperClass
interface MyInterface

class MyClass : MySuperClass(), MyInterface, Serializable

여러 인터페이스를 단일 클래스로 구현할 수 있는 동안 여러 클래스 상속은 금지됩니다.

클래스가 확장(다른 클래스) 또는 구현(하나 또는 서버 인터페이스)될 때 사용하는 일반적인 구문은 다음과 같습니다.

class Child: InterfaceA, InterfaceB, Parent(), InterfaceZ

클래스 및 인터페이스의 순서는 중요하지 않습니다.

또한 확장 클래스에서는 괄호를 사용합니다. 괄호는 부모 클래스의 주 생성자를 가리킵니다.따라서 부모 클래스의 주 생성자가 인수를 사용할 경우 자녀 클래스도 해당 인수를 전달해야 합니다.

interface InterfaceX {
   fun test(): String
}

open class Parent(val name:String) {
    //...
}

class Child(val toyName:String) : InterfaceX, Parent("dummyName"){

    override fun test(): String {
        TODO("Not yet implemented")
    }
}

언급URL : https://stackoverflow.com/questions/48391217/extend-and-implement-at-the-same-time-in-kotlin