dongshan1811 2015-07-28 01:04
浏览 99
已采纳

在Go中从基类分配给继承类

I know Go doesn't have such OO concept, but let me borrow them just to make it easier to explain.

OO inherits allows generalizing behavior to a more abstract type or class, using a class and a subclass of it, where the subclass inherits all the member fields and behaviors of the parent class. Go doesn't have such concept, but can achieve the same paradigm, in which the article explains Inheritance and subclassing in Go.

My question is more on the assignment. Suppose I have a "base class" Vehicle and a "inherit class" Car, like the following:

type Vehicle struct {
    wheelCount int
}

type Car struct {
    Vehicle //anonymous field Vehicle
    Maker   string
}

In true OO, I can assign a Vehicle object into a Car object, but I haven't found a way to do that in Go. So my questions are,

  • Is it possible to do that?
  • Else, what's the Go way to write a "Car" constructor from a "Vehicle"? I know the canonical way is c := Car{Vehicle{4}, "Ford"}, but what if I have other member fields in Car, like Module etc, and I'd like only to initial the member fields from Vehicle, nothing else?

I've prepared something for you to start with, http://play.golang.org/p/dcs9r7vPjZ

Thanks

  • 写回答

1条回答 默认 最新

  • drvlf9739 2015-07-28 01:20
    关注

    I can assign a Vehicle object into a Car object

    Do you mean opposite? In OOP you cannot assign Vehicle to Car, but you can assign either Car or Van to Vehicle.

    If so, you can do this in Go, of course:

    var v Vehicle
    c := Car{Vehicle:Vehicle{1}}
    v = c.Vehicle
    fmt.Println(v) // prints {1}
    

    This example also demonstrates that you can initialise exactly the fields you want to. However, this type of object construction is quite limited so the construction method is what typically used to get constructor-like behaviour:

    func CreateVehicle(wheels int) Vehicle {
        return Vehicle{wheels}
    }
    
    func CreateCar(wheels int, maker string) Car {
        return Car{CreateVehicle(wheels), maker}
    }
    
    func main() {
        var v Vehicle
        c := CreateCar(4, "Ford")
        v = c.Vehicle
        fmt.Println(v)
    }
    

    BTW, the reference management can be tricky in Go if you normally work with reference types. The structs are always passed by value. To pass it by reference, use & to get reference and * to specify the byref parameter.

    So, this will not work:

    func (v Vehicle) SetNumberOfWeels(wheels int) {
        v.wheelCount = wheels
    }
    
    func main() {
        v := Vehicle{}
        v.SetNumberOfWeels(5)
        fmt.Println(v) // prints {0}
    }
    

    But this will:

    func (v *Vehicle) SetNumberOfWeels(wheels int) {
        v.wheelCount = wheels
    }
    
    func main() {
        v := Vehicle{}
        v.SetNumberOfWeels(5)
        fmt.Println(v) // prints {5}
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)