3.12. Kotlin interface

发布时间 : 2025-10-25 13:34:28 UTC      

Page Views: 10 views

Kotlin interface is similar to Java 8, using the interface keyword defines the interface, allowing the method to have a default implementation:

interfaceMyInterface{funbar()//unrealizedfunfoo(){// Implemented//Optional method bodyprintln("foo")}} 

3.12.1. Implementation interface #

A class or object can implement one or more interfaces.

classChild:MyInterface{overridefunbar(){//Method Body}} 

Example #

interfaceMyInterface{funbar()funfoo(){//Optional Method Body println("foo")}}classChild: MyInterface{overridefunbar(){//Method Bodyprintln("bar")}}funmain(args:Array<String>){valc=Child()c.foo();c.bar();} 

The output is as follows:

foo bar 

3.12.2. Properties in the interface #

The properties in the interface can only be abstract, initialization values are not allowed, and the interface does not hold property values. When implementing the interface, you must override the properties:

interfaceMyInterface{varname:String//name attribute, abstract}classMyImpl:MyInterface{overridevarname:String="runoob"//Overridden properties} 

Example #

interfaceMyInterface{varname:String//name attribute, abstractfunbar()funfoo(){//Optional Method Bodyprintln("foo")}}classChild:MyInterface{overridevarname: String="runoob"//Overridden properties overridefunbar(){//Method Body println("bar")}}funmain(args: Array<String>){valc=Child()c.foo();c.bar();println(c.name)} 

The output is as follows:

foo bar runoob 

3.12.3. Function rewriting #

When implementing multiple interfaces, you may encounter the problem that the same method inherits multiple implementations. For example:

Example #

interfaceA{funfoo(){print("A")}//Realized funbar()//unrealized, There is no method body, it is abstract}interfaceB{funfoo(){print("B")}//Realized funbar() {print("bar")}//Realized}classC:A{overridefunbar(){print ("bar")}//rewrite}classD:A,B{overridefunfoo(){super.foo() super.foo()}overridefunbar(){super.bar()}}funmain(args: Array){vald=D()d.foo();d.bar();}    

The output is as follows:

ABbar 

Both interfaces A and B define methods in the example foo() and bar() both have been realized foo() B realized bar() . BecauseC is a concrete class that implements A, it must be overridden bar() and implement this abstract method.

However, if we derive D from An and B, we need to implement all the methods inherited by multiple interfaces and indicate how D should implement them. This rule applies to inheriting a single implementation ( bar() ) can also be used to inherit multiple implementations ( foo() ).

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。