dongqiao1151 2018-01-27 11:52
浏览 16
已采纳

嵌套接口:X未实现Y(Z方法的类型错误)

In one package I have an interface Repository that has a method GetReporter that returns an interface Reporter. This is used by a function Execute that takes a Repository and gets its Reporter via the GetReporter function.

In another package I have a struct GithubRepository that has a method GetReporter that returns a GithubReporter.

In a third package I want to call the Execute function out of package #1 with a GithubRepository instance.

I am trying to have package 1 and 2 independent of each other, without one importing something from the other. The 3rd package should combine the first two.

Golang returns:

cannot use githubRepository (type GithubRepository) as type Repository in argument to Execute:
    GithubRepository does not implement Repository (wrong type for GetReporter method)
        have GetReporter(string) GithubReporter
        want GetReporter(string) Reporter

Code:

package main

// Package #1

type Repository interface {
  GetReporter(string) Reporter
}

type Reporter interface {
  ChangeStatus(string) error
}

func Execute(r Repository) {
  // Do something with the repository
}

// Package #2

type GithubRepository struct {
}

type GithubReporter struct {
}

func (repo *GithubRepository) GetReporter(sha string) GithubReporter {
 return GithubReporter{}
}

func (reporter *GithubReporter) ChangeStatus(status string) error {
  // Change the status
  return nil
}

// Package #3

func main() {
  githubRepository := GithubRepository{}
  Execute(githubRepository)
}

Go Playground: https://play.golang.org/p/ph0sZnyAC5I

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥50 关于github项目下载运行
      • ¥15 苹果手机删除了qq软件,qq文件怎么恢复
      • ¥20 用mips 人力编程排序问题(能在mars按要求运行)
      • ¥15 Verilog小型神经网络(单层反馈神经网络)
      • ¥15 设计一个数字钟,实现时分秒的显示
      • ¥15 Clion使用头文件链接两个cpp
      • ¥15 51单片机无法触发外部中断问题
      • ¥15 基于半监督,无监督的调制识别
      • ¥15 cst求 阵列 rcs 时候 频域 时域求解器 应该怎样设置 才让两者 计算结果 相似 请附图
      • ¥15 关于#51单片机#的问题:大家帮我看一下为什么我的计时器数字一直不稳定