English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Golang

Instructions de contrôle Golang

Fonction & Méthode Golang

Structure Golang

Slice & Tableau Golang

Chaîne (String) Golang

Pointeur Golang

Interface Golang

Concurrence Golang

Exceptions (Error) Golang

Autres articles Golang

Enchevêtrement d'interface du langage Go

En langage Go, une interface est une collection de signatures de méthodes, c'est aussi un type, ce qui signifie que vous pouvez créer des variables de type interface. Comme tout le monde le sait, le langage Go ne supporte pas l'héritage, mais les interfaces Go supportent pleinement l'enchaînement. Dans le processus d'enchaînement, une interface peut enchaîner d'autres interfaces, ou une interface peut enchaîner les signatures de méthodes d'autres interfaces, les résultats des deux sont identiques à l'exemple1et2中所示的相同。您可以在单个接口中嵌套任意数量的接口。而且,如果对接口的方法进行了任何更改,则在将一个接口嵌套其他接口时,该接口也将反映在嵌套式接口中,如示例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
}
// implémentation de l'interface1méthode
func (a author) details() {
    fmt.Printf("Auteur: %s", a.a_name)
    fmt.Printf("\nDépartement: %s Date de passage: %d", a.branch, a.year)
    fmt.Printf("\nNom du collège: %s", a.college)
    fmt.Printf("\nSalaire: %d", a.salary)
    fmt.Printf("\nNombre d'articles publiés: %d", a.particles)
}
// implémentation de l'interface2méthode
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNombre d'articles à déterminer: %d", pendingarticles)
}
func main() {
    // affectation de structure
    values := author{
        a_name: "Mickey",
        branch: "Informatique",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }
    // 使用FinalDetails接口访问接口1,2méthode
    var f FinalDetails = values
    f.details()
    f.articles()
}

Sortie :

Auteur : Mickey
Département : Informatique par date : 2012
Nom de l'université : XYZ
Salaire : 50000
Nombre d'articles publiés : 209
Nombre d'articles à déterminer : 100

Instructions d'utilisation :comme indiqué dans l'exemple ci-dessus, nous avons trois interfaces. L'interface1et2是简单接口,接口3是嵌套式接口,其中同时包含1et2接口。因此,如果接口1和接口2发生了任何变化,接口3都会反映出来。接口3peuvent accéder aux interfaces1et2中存在的所有方法。

接口方法嵌套:

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
}
// implémentation de l'interface1méthode
func (a author) details() {
    fmt.Printf("Auteur: %s", a.a_name)
    fmt.Printf("\nDépartement: %s Date de passage: %d", a.branch, a.year)
    fmt.Printf("\nNom du collège: %s", a.college)
    fmt.Printf("\nSalaire: %d", a.salary)
    fmt.Printf("\nNombre d'articles publiés: %d", a.particles)
}
// implémentation de l'interface2méthode
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNombre d'articles à déterminer: %d", pendingarticles)
}
func main() {
    // affectation de structure
    values := author{
        a_name: "Mickey",
        branch: "Informatique",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }

Sortie :

Auteur : Mickey
Département : Informatique par date : 2012
Nom de l'université : XYZ
Salaire : 50000
Nombre d'articles publiés : 209
Nombre d'articles à déterminer : 100

Instructions d'utilisation :comme indiqué dans l'exemple ci-dessus, nous avons trois interfaces. L'interface1et2是简单接口,接口3是嵌套式接口,在其中包含1et2接口方法签名。因此,如果接口1和接口2的方法发生任何更改,它将反映在接口3dans l'interface.3peuvent accéder aux interfaces1et2中存在的所有方法。

接口嵌套并同时拥有自己的方法的接口示例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()
}
// Structure 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
}
// implémentation de l'interface1méthode
func (a author) details() {
    fmt.Printf("Auteur: %s", a.a_name)
    fmt.Printf("\nDépartement: %s Date de passage: %d", a.branch, a.year)
    fmt.Printf("\nNom du collège: %s", a.college)
    fmt.Printf("\nSalaire: %d", a.salary)
    fmt.Printf("\nNombre d'articles publiés: %d", a.particles)
}
// implémentation de l'interface2méthode
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\nNombre d'articles à déterminer: %d", pendingarticles)
}
func (a author) picked() {
    fmt.Printf("\nNombre total d'articles sélectionnés: %d", a.pick)
}
// implémentation de la méthode intégrée à l'interface
func (a author) cdeatils() {
    fmt.Printf("\nIdentifiant de l'auteur: %d", a.cid)
    fmt.Printf("\nSoumission: %s", a.post)
}
func main() {
    //affectation de structure
    values := author{
        a_name: "Mickey",
        branch: "Informatique",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
        cid:       3087,
        post: "Écrivain de contenu technique",
        pick:      58,
    }
    // pour accéder à l'interface via l'interface FinalDetails1,2méthode
    var f FinalDetails = values
    f.details()
    f.articles()
    f.picked()
    f.cdeatils()
}

Sortie :

Auteur : Mickey
Département : Informatique par date : 2012
Nom de l'université : XYZ
Salaire : 50000
Nombre d'articles publiés : 209
Nombre d'articles à déterminer : 100
Nombre total d'articles sélectionnés : 58
Identifiant de l'auteur : 3087
Soumission: Écrivain de contenu technique

Instructions d'utilisation :comme indiqué dans l'exemple ci-dessus, nous avons trois interfaces. L'interface1et2est une interface simple, tandis que l'interface3est une interface imbriquée, qui contient l'interface1le signature de la méthode de l'interface2et ses propres méthodes. Par conséquent, si l'interface1et les méthodes de l'interface2Si tout change, cela se reflétera dans l'interface3dans l'interface.3peuvent accéder aux interfaces1Toutes les méthodes, y compris les interfaces1、2et ses propres méthodes.