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

Golang - Grundlegende Anleitung

Golang - Kontrollanweisungen

Golang - Funktion & Methode

Golang - Struktur

Golang - Slice & Array

Golang - Zeichenkette (String)

Golang - Zeiger

Golang - Schnittstellen

Golang - Parallelität

Ausnahmen (Error) von Golang

Andere Themen von Golang

Go Sprache Schnittstelle Einfügen

In der Programmiersprache Go sind Schnittstellen eine Sammlung von Methodensignaturen, die auch ein Typ sind, was bedeutet, dass Sie Variablen des Schnittstellen-Typs erstellen können. Bekanntlich unterstützt Go keine Vererbung, aber Go-Schnittstellen unterstützen die Vererbung vollumfänglich. Während des Vererbungsprozesses kann eine Schnittstelle andere Schnittstellen einbetten oder andere Schnittstellenmethodensignaturen in sich einbetten, und die Ergebnisse beider sind gleich wie im Beispiel1和2gezeigt. Sie können eine beliebige Anzahl von Interfaces in einem einzelnen Interface verschachteln. Und wenn Sie die Methoden des Interfaces ändern, wird dies auch im verschachtelten Interface widergespiegelt, wie im Beispiel gezeigt.3gezeigt.

Syntax:

type interface_name1 interface {
    Methode1())
}
type interface_name2 interface {
    Methode2())
}
type finalinterface_name interface {
    interface_name1
    interface_name2
}
oder
type interface_name1 interface {
    Methode1())
}
type interface_name2 interface {
    Methode2())
}
type finalinterface_name interface {
    Methode1())
    Methode2())
}

Interface-Verschachtelungsbeispiel1:

package main
import "fmt"
// Interface 1
type AuthorDetails interface {
    details()
}
// Interface 2
type AuthorArticles interface {
    articles()
}
// Interface 3
//Interface3verschachtelte Interface1und das Interface2
type FinalDetails interface {
    AuthorDetails
    AuthorArticles
}
// Struktur
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("Autor: %s", a.a_name)
    fmt.Printf("\nAbteilung: %s Durchführungsdatum: %d", a.branch, a.year)
    fmt.Printf("\nUniversitätsname: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func main() {
    // 结构体赋值
    values := author{
        a_name: "Mickey",
        branch: "Informatik",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }
    // FinalDetails-Interface verwenden, um auf das Interface1,2的方法
    var f FinalDetails = values
    f.details()
    f.articles()
}

输出:

作者: Mickey
部门: Informatik 通过日期: 2012
大学名称: XYZ
薪水: 50000
发表文章数: 209
待定文章数: 100

用法说明:如上例所示,我们有三个接口。接口1和2einfaches Interface, das Interface3ein verschachteltes Interface ist, das sowohl enthält1和2Interface.1und das Interface2verändert, wird das Interface3werden alle widergespiegelt. Wenn das Interface3可以访问接口1和2enthaltene alle Methoden.

Interface-Methos-Verschachtelung:

package main
import "fmt"
// Interface 1
type AuthorDetails interface {
    details()
}
// Interface 2
type AuthorArticles interface {
    articles()
}
// Interface 3
//Interface3 hat das Interface eingebettet1und Methoden des Interfaces
type FinalDetails interface {
    details()
    articles()
}
// Struktur
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("Autor: %s", a.a_name)
    fmt.Printf("\nAbteilung: %s Durchführungsdatum: %d", a.branch, a.year)
    fmt.Printf("\nUniversitätsname: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func main() {
    // 结构体赋值
    values := author{
        a_name: "Mickey",
        branch: "Informatik",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }

输出:

作者: Mickey
部门: Informatik 通过日期: 2012
大学名称: XYZ
薪水: 50000
发表文章数: 209
待定文章数: 100

用法说明:如上例所示,我们有三个接口。接口1和2einfaches Interface, das Interface3ein verschachteltes Interface ist, das enthält1和2Interface-Methos-Signatur widerspiegeln. Daher, wenn das Interface1und das Interface2Methoden ändern, wird es sich im Interface3中。接口3可以访问接口1和2enthaltene alle Methoden.

Beispiel eines Interfaces, das sowohl verschachtelt ist als auch seine eigenen Methoden hat3:

package main
import "fmt"
// Interface 1
type AuthorDetails interface {
    details()
}
// Interface 2
type AuthorArticles interface {
    articles()
    picked()
}
// Interface 3
//Interface3hat das Interface verschachtelt1und das Interface2,hinzugefügt hat seine eigenen Methoden
type FinalDetails interface {
    details()
    AuthorArticles
    cdeatils()
}
// author Struktur
type author struct {
    a_name string
    branch string
    college string
    year int
    salary int
    particles int
    tarticles int
    cid int
    post string
    pick int
}
// 实现接口1的方法
func (a author) details() {
    fmt.Printf("Autor: %s", a.a_name)
    fmt.Printf("\nAbteilung: %s Durchführungsdatum: %d", a.branch, a.year)
    fmt.Printf("\nUniversitätsname: %s", a.college)
    fmt.Printf("\n薪水: %d", a.salary)
    fmt.Printf("\n发表文章数: %d", a.particles)
}
// 实现接口2的方法
func (a author) articles() {
    pendingarticles := a.tarticles - a.particles
    fmt.Printf("\n待定文章数: %d", pendingarticles)
}
func (a author) picked() {
    fmt.Printf("\n所选文章的总数: %d", a.pick)
}
// 实现嵌入了接口的方法
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: "Informatik",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
        cid:       3087,
        post: "Technischer Inhaltsschreiber",
        pick:      58,
    }
    // 使用 FinalDetails 接口访问接口1,2的方法
    var f FinalDetails = values
    f.details()
    f.articles()
    f.picked()
    f.cdeatils()
}

输出:

作者: Mickey
部门: Informatik 通过日期: 2012
大学名称: XYZ
薪水: 50000
发表文章数: 209
待定文章数: 100
所选文章的总数: 58
作者Id: 3087
提交: Technischer Inhaltsschreiber

用法说明:如上例所示,我们有三个接口。接口1和2是简单接口,而接口3是嵌套式接口,其中包含接口1的方法签名,接口2和它自己的方法。因此,如果接口1的方法和接口2发生任何更改,它将反映在接口3中。接口3可以访问接口1所有方法,包括接口1、2und seine eigenen Methoden.