dongxia8656 2015-07-09 01:13
浏览 50

Golang:此类型实现哪种接口?

In essence, four questions are here for Golang interfaces, with each one slightly harder than the one before it.

  1. say we imported a lot of interfaces, A, B, C, ..., G
import (
    "A",
    "B",
    "C",
    // more ...
    "G"
)

and we define a type S:

type S struct {
    // more ...
}

now if S "implements" one of the interfaces:

func (s S) Foo() {
    // more ...
}

Up to date, is there any other way to tell which interface of A - G does S implement, except for looking and searching into interface declarations of A - G? If no, is there a way to explicitly tell S to implement which interface? So far, it appears that the which interface S implemented is inferred.

  1. If, for the same example, B, C, D interfaces all have the Foo() method with the same signature. In this case, which interface does S implement? Does it depend on the order those interfaces are imported from B, C, and D?

  2. If, rather than importing interfaces, we declare and define interfaces B, C, D in the same file, but those interfaces again all have Foo() method with the same signature, which interface does S implement? Does it depend on the order those interfaces are declared, or defined?

  3. If, we declared and defined B, C, D interfaces in the same file, B has Foo() method, C has Bar() method, and D has both Foo() and Bar(). Now if S implement Foo() and Bar(), then does S implement B and C, or S only implements D?

Thanks, everyone!

  • 写回答

1条回答

  • dongtan5555 2015-07-09 01:33
    关注
    1. No. (But as a side note, you don't import interfaces, you import packages.)

    2. All of them. Order doesn't matter. If you declared variables of type B, C, and D, all of them would be able to hold an S.

    3. All of them. What you do in the same or different files doesn't matter.

    4. All of them. S implements B because S has Foo(). S implements C because S has Bar(). S implements D because S has both Foo() and Bar().

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!