dongxi5505 2016-07-27 04:16
浏览 116
已采纳

我们在Golang中有与Java和C#对象相同的东西吗?

In Java and C#, the Object class is implicitly parent of all classes that are defined. Is there something similar in go?

  • 写回答

1条回答 默认 最新

  • dplase3140 2016-07-27 04:25
    关注

    There is no inheritance in Go.
    I think you are looking for interface: Go: What's the meaning of interface{}?

    but if you need something similar to Object (not Class) you may use interface:

    Variables of interface type also have a distinct dynamic type, which is the concrete type of the value assigned to the variable at run time (unless the value is the predeclared identifier nil, which has no type). The dynamic type may vary during execution but values stored in interface variables are always assignable to the static type of the variable.

    var x interface{}  // x is nil and has static type interface{}
    var v *T           // v has value nil, static type *T
    x = 42             // x has value 42 and dynamic type int
    x = v              // x has value (*T)(nil) and dynamic type *T
    

    and:

    Interface types:

    An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface. The value of an uninitialized variable of interface type is nil.

    see: http://golang.org/doc/go_spec.html

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

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面