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

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

接口作為結構中的字段,接口調用將輸入作為同一結構的方法

接口作為結構中的字段,接口調用將輸入作為同一結構的方法

Go
HUH函數 2023-08-07 14:38:28
就我而言,“RequestHandlerProxy”是一個結構,其字段為接口“IAdapter”,并且接口有許多要調用的方法,該方法的輸入為結構“RequestHandlerProxy”。請幫助我如何處理這個?如何定義結構“RequestHandlerProxy”的值并傳遞?以下是我的接口結構和方法: 接口“IAdapter”位于文件“adapters”中type RequestHandlerProxy struct {    TestMode       bool    coreInstanceId string    adapter        adapters.IAdapter    coreProxy      adapterif.CoreProxy}func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy adapterif.CoreProxy) *RequestHandlerProxy {    var proxy RequestHandlerProxy    proxy.coreInstanceId = coreInstanceId    proxy.adapter = iadapter    proxy.coreProxy = cProxy    return &proxy}func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) {    return new(empty.Empty), nil    }func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) {    return nil, nil}func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) {        return nil, nil}以下是我在適配器文件中的界面:type IAdapter interface {    Adapter_descriptor() error    Device_types() (*voltha.DeviceTypes, error)    Health() (*voltha.HealthStatus, error)}
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

您提供的代碼有點難以理解(不完整且目的不清楚),因此這里是一個簡化的示例,希望能夠回答您的問題。RequestHandlerProxy請注意,從問題標題來看,我假設實現該接口的事實IAdapter讓您感到困惑;這可能是無關緊要的(可能還有其他函數接受 an ,在其中傳遞 a或您自己的實現IAdapter是有意義的,如下所示)。RequestHandlerProxyIAdapteradaptImpl

我已簡化IAdapter為包含單個函數并將所有內容都放在一個文件中。要擴展它以在您的示例中工作,您將需要實現所有三個函數(Adapter_descriptor()Device_types()Health())。代碼可以在go Playground中運行(如果這不能回答您的問題,也許您可以修改該代碼以提供問題的簡化示例)。

package main

import "errors"


// IAdapter - Reduced to one function to make this simple

type IAdapter interface {

    Adapter_descriptor() error

}


/// NewRequestHandlerProxy - Simplified version of the New function

func NewRequestHandlerProxy(iadapter IAdapter) {

    return // code removed to make example simple

}


// adaptImpl - Implements the IAdapter interface

type adaptImpl struct {

    isError bool // example only

}


// Adapter_descriptor - Implement the function specified in the interface

func (a adaptImpl) Adapter_descriptor() error {

    if a.IsError {

        return errors.New("An error happened")

    }

    return nil

}


func main() {

    // Create an adaptImpl which implements IAdapter 

    x := adaptImpl{isError: true}

    NewRequestHandlerProxy(x)

}


查看完整回答
反對 回復 2023-08-07
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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