结构体定义:
和C++ 一样,Golang的结构体也是封装数据。可以说是面向对象吧。
结构体的组合函数:
package main import ( "fmt" ) type Node struct { x,y int } // 结构体外接函数(能不能在结构体内写,目前还不清楚能不能在内部定义 func (node Node) area() (res int) { res = 0 res = node.x * node.y return } func main() { var node = Node{1,2} fmt.Println(node.area()) }
结构体可以内嵌结构体类型的数据
接口:
和C++ 的虚函数类似(实现机制目前还不清楚)
package main import ( "fmt" ) // 定义接口 type Phone interface { // 定义方法 call() } type Iphone struct { } type Nokiaphone struct { } func (nokiaphone Nokiaphone) call() { fmt.Println("this is Nokiaphone") } func (iphone Iphone) call() { fmt.Println("this is Iphone") } func main() { var phone Phone phone = new(Iphone) phone.call() phone = new(Nokiaphone) phone.call() }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。