[2023] Kotlin Tutorial Part II Object-Oriented and Functional Programming Chapter 12 Abstract Classes and Interfaces 12.2 Using Interfaces 12.2.5 Concrete Functions and Properties in Interfaces

[2023] Kotlin Tutorial

insert image description here

Part II Object Oriented and Functional Programming

Chapter 12 Abstract Classes and Interfaces

A well-designed software system should have "reusability" and "scalability" to meet the continuous changes of user needs. The use of abstract classes and interfaces is an important design method to achieve "reusability" and "extensibility".

12.2 Using the interface

Interfaces are more abstract than abstract classes . Interfaces should mainly contain abstract functions and attributes, but they can have specific functions and attributes as needed.

[Hint] Both interfaces and abstract classes can have abstract functions and properties, as well as concrete functions and properties. So what is the difference between an interface and an abstract class? An interface cannot maintain an object state, but an abstract class can , because maintaining an object state requires supporting fields, and there are no supporting fields in the interface, whether it is a concrete attribute or an abstract attribute.

12.2.5 Concrete functions and properties in the interface

The main members of interfaces in Kotlin are abstract functions and properties, but there are also concrete functions and properties. The abstract functions and properties in the interface must be implemented, while the specific functions and properties are optional . You can choose whether to rewrite them according to your business needs.

InterfaceA interface code:

interface InterfaceA {
    
    
    fun methodA()
    fun methodB(): String

    fun methodC(): Int {
    
    
        return 0
    }

    fun methodD(): String {
    
    
        return "这是默认函数..."
    }

}

Two abstract functions methodA and methodB, and two concrete functions methodC and methodD are declared in the interface InterfaceA, and the specific implementation is given.

Example code to implement the interface:

class ABC : InterfaceA {
    
    

    override fun methodA() {
    
    }

    override fun methodB(): String {
    
    
        return "实现methodB 函数"
    }

    override fun methodC(): Int {
    
    
        return 500
    }

}

When implementing an interface, the original abstract functions in the interface must be implemented in the implementing class. Specific functions can be selectively rewritten as needed. In the above code, the ABC class implements the two abstract functions in the InterfaceA interface, and rewrites methodB.

Call code:

fun main() {
    
    

    val abc = ABC()

    println(abc.methodB())

    println(abc.methodC())

    println(abc.methodD())
}

insert image description here

It can be seen from the running results that calling the function methodC is the implementation in the calling class ABC. Calling function methodD is the implementation of calling interface InterfaceA.

Guess you like

Origin blog.csdn.net/weixin_44226181/article/details/130004451