dspv70887 2013-06-03 13:29
浏览 170

如何在golang中创建自动调用的结构方法?

I have to create a something like a substitute of 2 levels of inheritance in golang, i.e., in a package, I have a structure(A), which is inherited(embedded as an anonymous field) by another structure(B) in another package, whose object is to be utilized by the "main" package.

Now, I've created an initializer method for the "B" (BPlease) that returns an object of B (say,B_obj). I can call this initializer(BPlease) from my "main" package at the start of the program.

One of the methods of "B" (say, HelloB()), calls a method of "A"(say,HelloA()) during execution, using "B's" object.

But what I really want is, something like a constructor for "A" that can initialize its fields (preferably when B_obj was created in package "main") before "B" calls any methods of "A".

How to achieve this?

I tried creating an initializer(APlease) for "A" as well and called it (BPlease) to get an object of "A" (A_obj). But I found this object useless as I couldn't utilize it to call "A's" method (HelloA()) inside a method of "B" (HelloB()). It would be great if someone can tell me how to utilize this object (A_obj).

Here's some code to clarify my query:

    package A
    type A struct { whatever }
    func (A_obj *A) HelloA(){performs some operation...}           // a method of A()
    func APlease() A {
          return A{initialize A fields}
        }
-------------------------------------------------------------------------------
    package B
    type B struct {
      A
      B fields
    }

    func BPlease() B {
      return B{
      A_obj := APlease()                     // useless to me.... how to utilise this?
      initialize B fields}
    }

    func (B_obj *B) HelloB(){                            // a method of B{}
      call B_obj.HelloA()                         // valid as A is an anon field in B struct
      some other operations                       // but A's fields are not initialized for B_obj
...}         

---------------------------------------------------
package main

import "B"
import "A"

func main(){
  B_obj := B.BPlease()         // what I want is, calling this should initialize A's fields for B_obj as well so that when HelloB() calls B_obj.HelloA(), it utilises A's field that have been initialized.
}

I cannot pass all field-values as parameters to B_obj as there are a lot of fields, and also, some field values are generated by calling a method of the same structure.

  • 写回答

4条回答 默认 最新

  • 普通网友 2013-06-03 13:48
    关注

    Regardless of anyone's opinion about fighting the language to have inheritance when it doesn't: No, there are no magic methods, like "getter" or "setter" of whatever. Remotely related (magic powers) are perhaps finalizers, but they're surely not going to help in this case.

    However, let me suggest to stop coding language X in Go. Just use Go. Go doesn't use "class-like" inheritance, nor a Go programmer (usually) should. Think of Go like a modernized C. There's no much C code out there relying on inheritance. (OK, I know about GObject ;-)

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名