在Go語言中,接口是方法簽名的集合,它也是一種類型,意味著您可以創(chuàng)建接口類型的變量。眾所周知,Go語言不支持繼承,但是Go接口完全支持嵌套。在嵌套過程中,一個接口可以嵌套其他接口,或者一個接口可以在其中嵌套其他接口的方法簽名,兩者的結果與示例1和2中所示的相同。您可以在單個接口中嵌套任意數(shù)量的接口。而且,如果對接口的方法進行了任何更改,則在將一個接口嵌套其他接口時,該接口也將反映在嵌套式接口中,如示例3所示。
語法:
type interface_name1 interface {
Method1()
}
type interface_name2 interface {
Method2()
}
type finalinterface_name interface {
interface_name1
interface_name2
}
或
type interface_name1 interface {
Method1()
}
type interface_name2 interface {
Method2()
}
type finalinterface_name interface {
Method1()
Method2()
}接口嵌套示例1:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
details()
}
// 接口 2
type AuthorArticles interface {
articles()
}
// 接口 3
//接口3嵌套接口1和接口2
type FinalDetails interface {
AuthorDetails
AuthorArticles
}
// 結構體
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
}
// 實現(xiàn)接口1的方法
func (a author) details() {
fmt.Printf("作者: %s", a.a_name)
fmt.Printf("\n部門: %s 通過日期: %d", a.branch, a.year)
fmt.Printf("\n大學名稱: %s", a.college)
fmt.Printf("\n薪水: %d", a.salary)
fmt.Printf("\n發(fā)表文章數(shù): %d", a.particles)
}
// 實現(xiàn)接口2的方法
func (a author) articles() {
pendingarticles := a.tarticles - a.particles
fmt.Printf("\n待定文章數(shù): %d", pendingarticles)
}
func main() {
// 結構體賦值
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012,
salary: 50000,
particles: 209,
tarticles: 309,
}
// 使用FinalDetails接口訪問接口1,2的方法
var f FinalDetails = values
f.details()
f.articles()
}輸出:
作者: Mickey 部門: Computer science 通過日期: 2012 大學名稱: XYZ 薪水: 50000 發(fā)表文章數(shù): 209 待定文章數(shù): 100
用法說明:如上例所示,我們有三個接口。接口1和2是簡單接口,接口3是嵌套式接口,其中同時包含1和2接口。因此,如果接口1和接口2發(fā)生了任何變化,接口3都會反映出來。接口3可以訪問接口1和2中存在的所有方法。
接口方法嵌套:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
details()
}
// 接口 2
type AuthorArticles interface {
articles()
}
// 接口 3
//接口3 嵌入了接口1和接口的方法
type FinalDetails interface {
details()
articles()
}
// 結構體
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
}
// 實現(xiàn)接口1的方法
func (a author) details() {
fmt.Printf("作者: %s", a.a_name)
fmt.Printf("\n部門: %s 通過日期: %d", a.branch, a.year)
fmt.Printf("\n大學名稱: %s", a.college)
fmt.Printf("\n薪水: %d", a.salary)
fmt.Printf("\n發(fā)表文章數(shù): %d", a.particles)
}
// 實現(xiàn)接口2的方法
func (a author) articles() {
pendingarticles := a.tarticles - a.particles
fmt.Printf("\n待定文章數(shù): %d", pendingarticles)
}
func main() {
// 結構體賦值
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012,
salary: 50000,
particles: 209,
tarticles: 309,
}輸出:
作者: Mickey 部門: Computer science 通過日期: 2012 大學名稱: XYZ 薪水: 50000 發(fā)表文章數(shù): 209 待定文章數(shù): 100
用法說明:如上例所示,我們有三個接口。接口1和2是簡單接口,接口3是嵌套式接口,在其中包含1和2接口方法簽名。因此,如果接口1和接口2的方法發(fā)生任何更改,它將反映在接口3中。接口3可以訪問接口1和2中存在的所有方法。
接口嵌套并同時擁有自己的方法的接口示例3:
package main
import "fmt"
// 接口 1
type AuthorDetails interface {
details()
}
// 接口 2
type AuthorArticles interface {
articles()
picked()
}
// 接口 3
//接口3嵌套了接口1和接口2,同時加入了自己的方法
type FinalDetails interface {
details()
AuthorArticles
cdeatils()
}
// author 結構體
type author struct {
a_name string
branch string
college string
year int
salary int
particles int
tarticles int
cid int
post string
pick int
}
// 實現(xiàn)接口1的方法
func (a author) details() {
fmt.Printf("作者: %s", a.a_name)
fmt.Printf("\n部門: %s 通過日期: %d", a.branch, a.year)
fmt.Printf("\n大學名稱: %s", a.college)
fmt.Printf("\n薪水: %d", a.salary)
fmt.Printf("\n發(fā)表文章數(shù): %d", a.particles)
}
// 實現(xiàn)接口2的方法
func (a author) articles() {
pendingarticles := a.tarticles - a.particles
fmt.Printf("\n待定文章數(shù): %d", pendingarticles)
}
func (a author) picked() {
fmt.Printf("\n所選文章的總數(shù): %d", a.pick)
}
// 實現(xiàn)嵌入了接口的方法
func (a author) cdeatils() {
fmt.Printf("\n作者Id: %d", a.cid)
fmt.Printf("\n提交: %s", a.post)
}
func main() {
//結構體賦值
values := author{
a_name: "Mickey",
branch: "Computer science",
college: "XYZ",
year: 2012,
salary: 50000,
particles: 209,
tarticles: 309,
cid: 3087,
post: "Technical content writer",
pick: 58,
}
// 使用 FinalDetails 接口訪問接口1,2的方法
var f FinalDetails = values
f.details()
f.articles()
f.picked()
f.cdeatils()
}輸出:
作者: Mickey 部門: Computer science 通過日期: 2012 大學名稱: XYZ 薪水: 50000 發(fā)表文章數(shù): 209 待定文章數(shù): 100 所選文章的總數(shù): 58 作者Id: 3087 提交: Technical content writer
用法說明:如上例所示,我們有三個接口。接口1和2是簡單接口,而接口3是嵌套式接口,其中包含接口1的方法簽名,接口2和它自己的方法。因此,如果接口1的方法和接口2發(fā)生任何更改,它將反映在接口3中。接口3可以訪問接口1中的所有方法,包括接口1、2和它自己的方法。