Golang 创建一个接口

Golang 创建一个接口

在本文中,我们将学习如何使用 Golang 编程创建一个接口。

接口 − 在 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包,允许我们在屏幕上打印任何内容。

  • 创建一个临时结构,其中包含一个键为count的值。

  • 创建一个名为inter的接口,并在其中定义方法签名。

  • 为返回整数值的count键定义名为getCount()的函数。

  • 现在,我们需要为接口类型定义一个变量。

  • 将此变量分配给上面定义的结构体,这将让我们可以使用接口中定义的方法。

  • 创建一个结构的实例,并将整数作为值提供给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 接口中定义的方法。

  • 通过这种方式,我们可以调用在其他接口上定义的方法,但不是其一部分。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程