First published on public account: DSGtalk1989

7.Kotlin interface

  • The default implementation

    Kotlin supports default implementation of interface methods, as well as overwriting interface attributes

    Properties in interfaces cannot be initialized, but they can still be customized by rewriting accessors

    interface MyInterface {
         val propertyWithImplementation: String
             get() = "2"
     }
    Copy the code
  • Same interface method

    class D : A, B {
      override fun foo() {
              super<A>.foo()
              super<B>.foo()
          }
       
          override fun bar() {
              super<B>.bar()
          }
      }
    Copy the code

    The A and B interfaces define the interface method foo, and the implementation class can call the default method implemented in the interface via super<>