亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何實現具有相同方法名稱和不同參數的兩個接口

如何實現具有相同方法名稱和不同參數的兩個接口

Go
料青山看我應如是 2022-12-05 17:20:43
我有兩個不同的接口(來自兩個不同的包)我想實現。但是他們有沖突,就像這樣:type InterfaceA interface {  Init()}type InterfaceB interface {  Init(name string)}type Implementer struct {} // Wants to implement A and Bfunc (i Implementer) Init() {}func (i Implementer) Init(name string) {} // Compiler complains它說“方法重新聲明”。一個結構如何實現兩個接口?
查看完整描述

3 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

正如已經回答的那樣,這是不可能的,因為 Golang 不(并且可能不會)支持方法重載。

查看Golang 常見問題解答

使用其他語言的經驗告訴我們,具有相同名稱但不同簽名的各種方法偶爾有用,但在實踐中也可能令人困惑和脆弱。僅按名稱匹配并要求類型一致是 Go 類型系統中的一個主要簡化決策。


查看完整回答
反對 回復 2022-12-05
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

這不可能。

在 go 中,你必須有一個單一的方法簽名。

您應該重命名一種方法。



查看完整回答
反對 回復 2022-12-05
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

方法簽名必須匹配。如果你想要依賴注入,我會推薦功能選項模式。功能選項是返回在構造函數的循環中調用的其他函數的函數。這是一個如何使用功能選項和接口基礎知識的示例。


package main


import (

    "fmt"

    "strconv"

)

type SomeData struct {

    data string

}

// SomeData and SomeOtherData both implement SomeInterface and SomeOtherInterface

// SomeInterface and SomeOtherInterface both implement each other.

type SomeInterface interface {

    String() string

    Set(data string)


}


func (s *SomeData)String() string {

    return s.data

}


func (s *SomeData)Set(data string)  {

    s.data = data

}


// SetDataOption is a functional option that can be used to inject a constructor dep

func SetDataOption(data string) func(*SomeData) {

   return func(s *SomeData) {

       s.Set(data)

   }

}

// NewSomeData is the constructor; it takes in 0 to many functional options and calls each one in a loop.

func NewSomeData(options ...func(s *SomeData)) SomeInterface {

   s := new(SomeData)


   for _, o := range options {

       o(s)

   }

   return s

}


//********************

type SomeOtherData struct {

    data string

    i    int

}


type SomeOtherInterface interface {

    String() string

    Set(data string)


}



func (s *SomeOtherData)String() string {

    return s.data + "  " + strconv.Itoa(s.i)

}


func (s *SomeOtherData)Set(data string)  {

    s.data = data

}



func SetOtherDataOption(data string) func(*SomeOtherData) {

   return func(s *SomeOtherData) {

      s.Set(data)

   }

}


func SetOtherIntOption(i int) func(*SomeOtherData) {

    return func(s *SomeOtherData) {

        s.i = i

    }

 }



// NewSomeOther data works just like NewSomeData only in this case, there are more options to choose from

// you can use none or any of them.

func NewSomeOtherData(options ...func(s *SomeOtherData)) SomeOtherInterface {

   s := new(SomeOtherData)


   for _, o := range options {

       o(s)

   }

   return s

}


//*********************************

// HandleData accepts an interface

// Regardless of which underlying struct is in the interface, this function will handle 

// either by calling the methods on the underlying struct.

func HandleData(si SomeInterface) {

    fmt.Println(si)  // fmt.Println calls the String() method of your struct if it has one using the Stringer interface

}


func main() {

    someData := NewSomeData(SetDataOption("Optional constructor dep"))

    someOtherData := NewSomeOtherData(SetOtherDataOption("Other optional constructor dep"), SetOtherIntOption(42))

    HandleData(someData) // calls SomeData.String()

    HandleData(someOtherData) // calls SomeOtherData.String()

    someOtherData = someData // assign the first interface to the second, this works because they both implement each other.

    HandleData(someOtherData) // calls SomeData.String()  because there is a SomeData in the someOtherData variable.

    

}


查看完整回答
反對 回復 2022-12-05
  • 3 回答
  • 0 關注
  • 233 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號