doudi2005 2018-08-01 00:41
浏览 31
已采纳

接口实现中的非接口方法

I have an interface which defines a method. I have a struct which implements this interface. In it, I have implemented the methods from this interface and also have additional methods defined.

For example:

package main

import (
    "fmt"
)   

type Animal interface {
    MakeNoise()
}

type Dog struct {
    color string
}

/* Interface implementation */

func (d *Dog) MakeNoise() {
    fmt.Println("Bark!")
}

/* End Interface implementation */

func (d *Dog) WagTail() {
    fmt.Println(d.color + " dog: Wag wag")
}

func NewDog(color string) Animal {
    return &Dog{color}
}

func main() {
    dog := NewDog("Brown")
    dog.MakeNoise()
    dog.WagTail()

}

On Playground: https://play.golang.org/p/B1GgoNToNl_l

Here, WagTail() is not part of the Animal interface but belongs to the Dog struct. Running this code gives an error

dog.WagTail undefined (type Animal has no field or method WagTail).

Is there a way I could have a struct adhere to an interface and also define it's own methods?

  • 写回答

3条回答 默认 最新

  • doushiposong30622 2018-08-01 07:10
    关注

    The error described it all:

    dog.WagTail undefined (type Animal has no field or method WagTail)

    To implement an interface you should implement all methods defined inside it.

    dog := NewDog("Brown")
    dog.MakeNoise()
    dog.WagTail()
    

    Now NewDog returns Animal interface which contains MakeNoise method but not WagTail.

    The only way to manage your requirement is either create a variable of struct type Dog and then you can call any method having Dog as receiver.

    d := &Dog{"Brown"}
    d.WagTail()
    

    Or you can return the pointer to Dog struct from NewDog method just like you did in your code mentioned in the comment as:

    func NewDog(color string) *Dog {
        return &Dog{color}
    }
    

    But if the method is not defined in interface you cannot implement it using the struct as method receiver.

    Golang provides a way in which:

    You can ask the compiler to check that the type T implements the interface I by attempting an assignment using the zero value for T or pointer to T, as appropriate

    type T struct{}
    var _ I = T{}       // Verify that T implements I.
    var _ I = (*T)(nil) // Verify that *T implements I.
    

    If T (or *T, accordingly) doesn't implement I, the mistake will be caught at compile time.

    If you wish the users of an interface to explicitly declare that they implement it, you can add a method with a descriptive name to the interface's method set. For example:

    type Fooer interface {
        Foo()
        ImplementsFooer()
    }
    

    A type must then implement the ImplementsFooer method to be a Fooer, clearly documenting the fact and announcing it in godoc's output.

    type Bar struct{}
    func (b Bar) ImplementsFooer() {}
    func (b Bar) Foo() {}
    

    Most code doesn't make use of such constraints, since they limit the utility of the interface idea. Sometimes, though, they're necessary to resolve ambiguities among similar interfaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线