2.42. Go language interface

发布时间 : 2025-10-25 13:32:56 UTC      

Page Views: 9 views

Go language provides another data type, the interface, which definesall common methods together, and any other type implements this interface as long as they implement these methods.

2.42.1. Example #

/* Define interfaces */ type interface_name interface { method_name1 [return_type] method_name2 [return_type] method_name3 [return_type] ... method_namen [return_type] } /* Define Structure */ type struct_name struct { /* variables */ } /* Implement interface methods */ func (struct_name_variable struct_name) method_name1() [return_type] { /* Method implementation */ } ... func (struct_name_variable struct_name) method_namen() [return_type] { /* Method implementation*/ } 

2.42.2. Example #

package main import ( "fmt" ) type Phone interface { call() } type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } type IPhone struct { } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") } func main() { var phone Phone phone = new(NokiaPhone) phone.call() phone = new(IPhone) phone.call() } 

In the above example, we define an interface Phone , there is a method inthe interface call() . And then we’re here. main function definesa Phone type variable and assign it a value of NokiaPhone and IPhone . And then call call() method, and the output is as follows:

I am Nokia, I can call you! I am iPhone, I can call you! 
《地理信息系统原理、技术与方法》  97

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