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

Golang 기본 튜토리얼

Golang 제어문

Golang 함수 & 메서드

Golang 구조체

Golang 슬라이스 & 배열

Golang 문자열(String)

Golang 포인터

Golang 인터페이스

Golang 병렬

Golang 예외(Error)

Golang 다른杂项

Go 언어 인터페이스 내장

Go 언어에서 인터페이스는 메서드 서명의 집합으로, 또한 한 종류로, 인터페이스 타입의 변수를 생성할 수 있습니다. 잘 알려져 있듯이 Go 언어는 상속을 지원하지 않지만, Go 인터페이스는 완전히 중첩을 지원합니다. 중첩 과정에서, 하나의 인터페이스는 다른 인터페이스를 중첩할 수 있으며, 또한 하나의 인터페이스는 다른 인터페이스의 메서드 서명을 중첩할 수 있습니다. 두 결과는 예제와 동일합니다.1와2와 같이 표시됩니다. 하나의 인터페이스에 여러 인터페이스를 내장할 수 있습니다. 그리고 인터페이스 메서드가 어떤 변경이든 발생하면, 다른 인터페이스에 내장할 때 이 인터페이스도 내장 인터페이스에 반영됩니다. 예제와 같이.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
}
// 인터페이스 구현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게시된 문서 수: %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: "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
게시된 문서 수: 209
미정 문서 수: 100

사용 설명:위 예제와 같이, 우리는 세 개의 인터페이스를 가지고 있습니다. 인터페이스1와2이 간단 인터페이스 인터페이스3이 내장 인터페이스를 포함한 내장 인터페이스 인터페이스1와2인터페이스.1와 인터페이스2이 어떤 변경이든 발생하면, 인터페이스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
}
// 인터페이스 구현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게시된 문서 수: %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: "Computer science",
        college: "XYZ",
        year:      2012,
        salary:    50000,
        particles: 209,
        tarticles: 309,
    }

출력:

작성자: Mickey
학과: Computer science 통합 날짜: 2012
대학 이름: XYZ
급여: 50000
게시된 문서 수: 209
미정 문서 수: 100

사용 설명:위 예제와 같이, 우리는 세 개의 인터페이스를 가지고 있습니다. 인터페이스1와2이 간단 인터페이스 인터페이스3이 내장 인터페이스를 포함한 내장 인터페이스 인터페이스1와2인터페이스 메서드 이름. 따라서, 인터페이스1와 인터페이스2메서드가 어떤 변경이든 발생하면, 인터페이스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
}
// 인터페이스 구현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게시된 문서 수: %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: "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
게시된 문서 수: 209
미정 문서 수: 100
선택된 문서의 총 수: 58
작성자 ID: 3087
제출: Technical content writer

사용 설명:위 예제와 같이, 우리는 세 개의 인터페이스를 가지고 있습니다. 인터페이스1와2는 간단한 인터페이스이며, 인터페이스3는 내장된 인터페이스를 포함한 내장형 인터페이스입니다.1의 메서드 식별자2그리고 자신의 메서드가 있습니다. 따라서, 인터페이스1의 메서드와 인터페이스2어떤 변경이 발생하면, 이는 인터페이스에 반영됩니다.3중.3인터페이스에 접근할 수 있습니다.1의 모든 메서드, 인터페이스 포함1、2그리고 자신의 메서드.