doutang2017 2016-09-23 14:24
浏览 10

如何基于属性更改在golang中调用函数?

Sample code : https://play.golang.org/p/vl3xwMWf5G

In the above code, I am making sure that, we don't unnecessarily call the LocateBasket function. It is called only once during the GetBasketCall. But if there is any attribute change (For eg. The quantity was changed to 30) then I wanted to make sure that when user calls GetBasket it internally calls LocateBasket function too.

In my example I had only one function, but if there is multiple attributes which needs to be re-calculated (based on attribute change) when their corresponding Getter Functions are getting called, what is the best approach to do the same.

  • 写回答

1条回答 默认 最新

  • donglu4633 2016-09-23 14:46
    关注

    Write a setter method, and either immediately LocateBasket or flag it for GetBasket to do:

    func (ball *Ball) SetQuantity(n int){
      ball.Quantity = n
      // ball.LocateBasket(ball)
      ball.dirty = true
    }
    
    func (ball *Ball) GetBasket() BasketType {
      if ball.dirty{
        ball.LocateBasket(ball)
        ball.dirty = false
        return ball.basket
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题