dor2p0520 2015-08-20 18:36
浏览 22
已采纳

重命名类型后,我无法访问其某些方法

In order to prevent having multiple dependencies along different files of my project and since I might change how data will be presented, I decided to make an "interface" (not in the golang sense but in an architectural way) to the draw2d package

As I didn't need anything else, I just renamed one of the types:

type CanvasContext draw2dimg.GraphicContext

In one of my modules I had the following code (path is a CanvasContext variable):

// initialization and some code omitted for clarity
path.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
path.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
path.SetLineWidth(5)
// some more code here
path.Close()
path.FillStroke()

Among all those method calls on path, only the FillStroke failed with the compile error:

path.FillStroke undefined (type *drawing.CanvasContext has no field or method FillStroke)

In order to prevent it I have to redefine the FillStroke, but not any other method, with:

func (cc *CanvasContext) FillStroke() {
    gc := draw2dimg.GraphicContext(*cc)
    gc.FillStroke()
}

Why do I have to redefine only that one and not any of the other calls?

  • 写回答

1条回答 默认 最新

  • duanbiao4025 2015-08-21 01:23
    关注

    you should use struct embedding rather than type define. check the struct 'embedding' document:

    but then to promote the methods of the fields and to satisfy the io interfaces, we would also need to provide forwarding methods, like this:

    func (rw *ReadWriter) Read(p []byte) (n int, err error) { return rw.reader.Read(p) }

    By embedding the structs directly, we avoid this bookkeeping.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包