최고의 언어 인터페이스로 이동 튜토리얼 2024년, 이 튜토리얼에서는 언어 인터페이스로 이동를 배울 수 있습니다.
이동 언어 함께 모든 방법의 정의와 공통점이 인터페이스를 또 다른 데이터 유형을 제공하고, 임의의 다른 유형은 이러한 방법을 구현하고,이 인터페이스를 구현하는 것이다.
/* 定义接口 */ type interface_name interface { method_name1 [return_type] method_name2 [return_type] method_name3 [return_type] ko. method_namen [return_type] } /* 定义结构体 */ type struct_name struct { /* variables */ } /* 实现接口方法 */ func (struct_name_variable struct_name) method_name1() [return_type] { /* 方法实现 */ } ko. func (struct_name_variable struct_name) method_namen() [return_type] { /* 方法实现*/ }
package main import ( "fmt" ) type Phone interface { call() } type NokiaPhone struct { } func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!") } type IPhone struct { } func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!") } func main() { var phone Phone phone = new(NokiaPhone) phone.call() phone = new(IPhone) phone.call() }
위의 예에서는 인터페이스 폰, 메소드 호출이있는 인터페이스 ()를 정의한다. 그 다음 우리는 내부의 주요 기능 형 전화에서 변수를 정의하고 누구에게 NokiaPhone과 아이폰에 할당되었다. 그런 다음 호출 () 메서드를 호출하여 다음과 같이 출력 결과는 다음과 같습니다
I am Nokia, I can call you! I am iPhone, I can call you!