Go语言程序创建接口
在本文中,我们将学习如何使用 Go 语言创建接口。
接口 - 在 Go 语言中,接口是一种自定义类型,用于指定一个或多个方法签名。接口是抽象的,即我们不能创建接口的实例,但我们可以为接口类型创建一个变量,并将此变量分配给具有接口所需方法的结构体或类。
语法
type interface_name interface {
// method signatures
}
示例 1
要定义一个接口,首先我们需要使用 type 关键字,它表示我们正在定义一个新类型。我们需要确定接口的名称,后跟关键字 interface,然后需要在此接口内提及方法签名。
package main
import "fmt"
// package fmt allows us to print anything on the screen
// creating a structure named temp and assigning it a property of count
type temp struct {
count int
}
// creating an interface named inter
type inter interface {
// defining method signatures in the interface
getCount() int
}
// defining and creating a function to the structure that returns an integer value
func (t temp) getCount() int {
// returning the value of the count
return t.count
}
func main() {
// creating a variable to the interface type
var struct_intr inter
// creating a new instance to the struct and assigning value to it
st := temp{98}
// Assigning the interface variable to the struct so that we could use the functions defined in
// the interface
struct_intr = st
// accessing the getCount() method from the instance to the struct
fmt.Println("Accessing the getCount() method from the st instance \ncount=", st.getCount(), "\n")
// priting the count by calling the getCount() method from the struct_inter interface variable
fmt.Println("Accessing the getCount() method from the interface variable\ncount=", struct_intr.getCount(), "\n")
}
输出
Accessing the getCount() method from the st instance count= 98 Accessing the getCount() method from the interface variable count= 98
描述
导入 fmt 包,该包允许我们在屏幕上打印任何内容。
创建一个名为 temp 的结构体,其中 count 为键。
创建一个名为 inter 的接口并在其中定义方法签名。
为结构体定义 getCount() 函数,该函数返回 count 作为整数值。
现在我们需要为接口类型定义一个变量。
将此变量分配给上面定义的结构体,这将允许我们从结构体的实例中使用接口中定义的方法。
创建结构体的实例,并为 count 键提供一个整数作为值。
现在,我们可以从接口变量和结构体实例中访问 getCount() 方法。
通过首先从接口然后从结构体变量调用 getCount() 方法来打印 count 的值。
示例 2
使用类型断言获取具体类型的返回值,并可以在其上调用定义在另一个接口上的方法,但不是满足接口的一部分。
package main
// defining the package’s main
// fmt package allows us to print anything on the screen
import "fmt"
// defining an interface named Polygons and defining method signatures in it
type Polygons interface {
// defining a method signature named Perimeter()
Perimeter()
}
// defining an interface named Object and defining a method in it too
type Object interface {
NumberOfSide()
}
// creating a struct named Pentagon that stores integer values.
type Pentagon int
// defining a method named Perimeter() to the int struct
func (p Pentagon) Perimeter() {
// printing the perimeter on the screen
fmt.Println("Perimeter of Pentagon", 5*p)
}
// defining a method named NumberOfSides() to the int struct
func (p Pentagon) NumberOfSide() {
// getting the number of sides of the pentagon
fmt.Println("A pentagon has 5 sides")
}
func main() {
// Giving the integer value to the pentagon struct
// further assigning the struct to the interface so that we could use the methods defined in it
var p Polygons = Pentagon(50)
// using the perimeter method to print the perimeter of the pentagon on the screen
p.Perimeter()
// assigning the instance to the struct so that we could use the methods present in it.
var o Pentagon = p.(Pentagon)
// using the NumberOfSides() method to calculate the number of sides of the pentagon
o.NumberOfSide()
// Giving the integer value to the pentagon struct
// further assigning the struct to the interface so that we could use the methods defined in it
var obj Object = Pentagon(50)
// using the NumberOfSide() method to print the perimeter of the pentagon on the screen
obj.NumberOfSide()
// printing the perimeter from the obj method
var pent Pentagon = obj.(Pentagon)
pent.Perimeter()
}
输出
Perimeter of Pentagon 250 A pentagon has 5 sides A pentagon has 5 sides Perimeter of Pentagon 250
描述
导入 fmt 包,该包允许我们在屏幕上打印任何内容。
创建一个名为 Pentagon 的新结构体,该结构体接受整数值。
创建两个名为 Polygon 和 object 的接口,并在其中定义方法签名。
为结构体定义 perimeter() 和 numberOfSides() 函数,它们将分别打印五边形的周长和边数。
将整数值赋予五边形结构体并将结构体分配给接口,以便我们可以使用其中定义的方法。
注意,我们正在使用在 obj 接口中定义的 numberOfSIdes(),而不是在 Polygon 中。
再次,将五边形 int 赋予 Pentagon 结构体并将其分配给 obj 接口。
同样,我们正在从 obj 接口上创建的实例中使用 Polygon 接口中定义的方法。
通过这种方式,我们可以调用定义在其他接口上的方法,但这些方法不是满足接口的一部分。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP