是否有可能在另一个包中实现一个带有未导出方法的接口? [英] Is it possible to implement an interface with unexported methods in another package?

查看:91
本文介绍了是否有可能在另一个包中实现一个带有未导出方法的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个会计系统访问接口。我想从我的程序中隐藏接口的特定实现,因为我将只有一个活动会计系统。所以我计划让接口的方法不导出(隐藏),然后导出基本包的本地函数,从本地适配器调用相同的函数。



<$















$ b var adapter IAdapter

func SetAdapter(一个IAdapter){
adapter = a
}

func GetInvoice()错误{
if(adapter == nil){
return errors.New(No adapter set!)
}
return adapter.getInvoice()
}


__________________________________________________

包裹帐户系统

类型适配器struct {}

func(一个适配器)getInvoice( )错误{return nil}


__________________________________________________


包裹主要

进口(
会计
accountingsystem


函数m ain(){
adapter:= accountingsystem.Adapter {}
accounting.SetAdapter(adapter)
}

问题在于编译器抱怨,由于无法通过<$ c查看 getInvoice()的实现$ c> accountingsystem.Adapter :

  ./ main.go:2:can not use adapter类型accountingsystem.Adapter)作为类型accounting.IAdapter在accounting.SetAdapter的参数:
accountingsystem.Adapter没有实现accounting.IAdapter(缺少accounting.getInvoice方法)
有accountingsystem.getInvoice()错误
想accounting.getInvoice()错误

是否有任何方法能够实现一个接口在另一个软件包中未导出的方法?或者我是用非惯用的方式来思考这个问题?

解决方案

字段,但不能提供您自己的未导出方法的实现。例如,此版本的Adapter满足accounting.IAdapter接口。

 类型适配器结构{
accounting.IAdapter



$ b

我无法使用适配器来提供我自己的IAdapter实现。 getInvoice()方法。



这个技巧不会帮助你。



如果你不想要其他软件包直接使用accountingsystem.Adapter,然后使类型不导出,并添加一个函数用于向适配器注册会计软件包。

 包计费

类型IAdapter接口{
GetInvoice()错误
}

---

包会计系统

类型适配器struct {}

func(一个适配器)GetInvoice()错误{return nil}

func SetupAdapter(){
accounting.SetAdapter(adapter {})
}

---

包主

func main(){
accounti ngsystem.SetupAdapter()
}


I have written an interface for accounting system access. I would like to hide specific implementations of the interface from my program as I will only ever have one "active" accounting system. So I planned on making the methods of the interface unexported (hidden), and then having exported functions native to the base package that call a the same function from a local adapter.

package accounting

import "errors"

type IAdapter interface {
    getInvoice() error
}

var adapter IAdapter

func SetAdapter(a IAdapter) {
    adapter = a
}

func GetInvoice() error {
    if (adapter == nil) {
        return errors.New("No adapter set!")
    }
    return adapter.getInvoice()
}


__________________________________________________

package accountingsystem

type Adapter struct {}

func (a Adapter) getInvoice() error {return nil}


__________________________________________________


package main

import (
    "accounting"
    "accountingsystem"
)

function main() {
    adapter := accountingsystem.Adapter{}
    accounting.SetAdapter(adapter)
}

The problem with this is that the compiler complains, due to not being able to see the implementation of getInvoice() by accountingsystem.Adapter:

./main.go:2: cannot use adapter (type accountingsystem.Adapter) as type accounting.IAdapter in argument to accounting.SetAdapter:
accountingsystem.Adapter does not implement accounting.IAdapter (missing accounting.getInvoice method)
    have accountingsystem.getInvoice() error
    want accounting.getInvoice() error

Is there any way of being able to implement an interface with unexported methods in another package? Or am I thinking about this problem in a non-idiomatic way?

解决方案

You can implement an interface with unexported methods using anonymous struct fields, but you cannot provide your own implementation of the unexported methods. For example, this version of Adapter satisfies the accounting.IAdapter interface.

type Adapter struct {
    accounting.IAdapter
}

There's nothing that I can do with Adapter to provide my own implementation of the IAdapter.getInvoice() method.

This trick is not going to help you.

If you don't want other packages to use accountingsystem.Adapter directly, then make the type unexported and add a function for registering the adapter with the accounting package.

package accounting

type IAdapter interface {
    GetInvoice() error
}

---

package accountingsystem

type adapter struct {}

func (a adapter) GetInvoice() error {return nil}  

func SetupAdapter() {
    accounting.SetAdapter(adapter{})
}

---

package main

func main() {
    accountingsystem.SetupAdapter()
}

这篇关于是否有可能在另一个包中实现一个带有未导出方法的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆